What is Exception Handling?
When we write a program, but by mistake if we wrote something wrong or if we get error at particular line then whole program will get hault. When at particular error program will get stop and code after that error will not execute.
To avoid this things, there is exception handling. It will avoid to hault whole program. Where there is error it will show error at only that place and after whole program will continue. This will possible by using try and catch method.
For Example:
class A{
int i=10/0; // exception created
System.out.println(i);
System.out.println(“Exception handling”);
}
Output: Error
As shown above, second statement does not execute due to error in first statement. This can be handled by using try and catch method.
For Example:
class A{
int i=10/0; // exception created
try{System.out.println(i);
}catch(Exception e){
System.out.println(e); }
System.out.println(“Hello”); }
Output: Error
Hello
Advantages
We can prevent the whole program from haulting.
Identifying Error types.
Propagation of Errors.