What is Exception in Java

What is Exception in java?

Before  reading this post kindly read about Error in Java.


Situation which we are able to handle inside the program is Exception. Exception is considered
as an issue or the problem that occurs in the program.

Exception is handled by using the below code in java

try{

// Exception occured in a program

}
catch(Exception e)
{

e.printStackTrace();

}


If any exception occurs, if that set of code is surrounded by try and catch, exception
will be catched and appropriate action will be taken.

In the above example, exception is catched and the trace details are printed.


try catch can be followed by finally as mentioned below

try{


}
catch{

}
finally{
 //To close connections
}



finally block will be used to close the connections or to execute the mandatory steps
if any exception occur.


finally should be immediately followed after the catch block. If any code is present in between
catch and finally ,error will be observed.




Previous
Next Post »