0% found this document useful (0 votes)
23 views

Exception

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Exception

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Exception:

-During the execution of java program JVM faces abnormal situation based on the code declaration.

-If JVM faces abnormal situation then trigger an event is known as exception.

-If exception event got generated in java program then it results in termination of java program.

Exception Handling:-

-Handling the event generated by JVM durig program execution is known as exception handling.

-We can handel exception by using try and catch block.

Ex:

try{

//Risky code

}catch{

//Event handled

Try block:

- Try block is use to declare risky code only.


- Controller visits try block only once throughout the execution of program.
- Try block should be followed by either caltch block or finally block.
- Multiple try blocks are not allowed.

Catch block:-

-It is use to handle event generated from try block.

-catch block will get executed only once if event generated in try(abnormal situation).

-catch block should be declare immediately after try block.

-Any number of catch block can be declare for single try.

What is difference between error and exception.

 Exception can be handled but error cannot handled.


Finally block:-

-this is use to close costly resources of current program.

-finally block should be followed by catch block.

-we can use finally block after try block but it not recomanded.

-finally block always executes irrespective of exception occurred or not.

What is difference between final, finally and finalize?

Final -> keyword

Ex: Final int a=10;

Finally -> block

Finalize -> method which is present in Garbage collection method.

package exception;

public class Test {

public static void main(String[] args) {

String [] ar = {"abc", "xyz", "pqrs" };

try {
System.out.println(ar[3]); //Risky code
}
catch (ArithmeticException a) {
System.out.println("Arithmatic Exception Handeled");
} catch (ArrayIndexOutOfBoundsException a) {
System.out.println("arry index handled");
a.printStackTrace();
} catch (NullPointerException a) {
System.out.println("Null pointer exception handled");
} finally {
System.out.println("finally block");
}

System.out.println("After risky code");


}

}
package exception;
public class Test1 {
public static void main(String[] args) {
int a=10;
int b=0;
System.out.println("11111-Before risky code");
try {
int c=a/b; //Risky code
System.out.println(c);
System.out.println("222222-After risky code in try block");

}catch (ArrayIndexOutOfBoundsException c) {
System.out.println("arry index handled");
}
catch(NullPointerException c) {
System.out.println("Null pointer exception handled");
}catch (Exception d){
System.out.println("Generic Exceoption");
}
finally {
System.out.println("finally block");

System.out.println("3333-After catch block code");


}

}
package exception;

public class Test2 {

public static void main(String[] args) {


String s = null;

try {
System.out.println(s.length()); //// Risky code
System.out.println("After risky code in try block");
}
catch (ArithmeticException e) {
System.out.println("Arithmatic Exception Handeled");
e.printStackTrace();

} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("arry index handled");
} catch (NullPointerException e) {
System.out.println("Null pointer exception handled");
e.printStackTrace();
}

System.out.println("after catch block");


int x=50;
int y=0;
try {
System.out.println(x/y);
}catch(Exception e) {
System.out.println("4444 - 2nd try block");

You might also like