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 {
[Link](ar[3]); //Risky code
}
catch (ArithmeticException a) {
[Link]("Arithmatic Exception Handeled");
} catch (ArrayIndexOutOfBoundsException a) {
[Link]("arry index handled");
[Link]();
} catch (NullPointerException a) {
[Link]("Null pointer exception handled");
} finally {
[Link]("finally block");
}
[Link]("After risky code");
}
}
package exception;
public class Test1 {
public static void main(String[] args) {
int a=10;
int b=0;
[Link]("11111-Before risky code");
try {
int c=a/b; //Risky code
[Link](c);
[Link]("222222-After risky code in try block");
}catch (ArrayIndexOutOfBoundsException c) {
[Link]("arry index handled");
}
catch(NullPointerException c) {
[Link]("Null pointer exception handled");
}catch (Exception d){
[Link]("Generic Exceoption");
}
finally {
[Link]("finally block");
[Link]("3333-After catch block code");
}
}
package exception;
public class Test2 {
public static void main(String[] args) {
String s = null;
try {
[Link]([Link]()); //// Risky code
[Link]("After risky code in try block");
}
catch (ArithmeticException e) {
[Link]("Arithmatic Exception Handeled");
[Link]();
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("arry index handled");
} catch (NullPointerException e) {
[Link]("Null pointer exception handled");
[Link]();
}
[Link]("after catch block");
int x=50;
int y=0;
try {
[Link](x/y);
}catch(Exception e) {
[Link]("4444 - 2nd try block");