Continuing with Previous topics :

What is Exception ?
What is Runtime – Error & Exception Java ?

This topic will be describing how to handle exception scenario in Java programming language.

Exception will be handled using try-catch only, or you can pass to caller.

 

Syntax for Handling Exception using try-catch

try{
       //code that may throw exception
catch(Exception_class_Name ref) {
}

 

Passing Handling scneario to caller using throws

method1 () throws Exception {
throw new Exception (“Exception Message”);
}

 

Here caller side while calling method1 either should handle or pass to next caller.

ex:

try {
    method1 () ;
catch(Exception ex) {
}

Advertisements