try catch finally in Java Programming

try catch finally in Java Programming

Previous Home Next

 

Try ,Catch,and finally blocks are used all together in a java program .
if error /exception occurs the catch  block is run and then finally block
and if error is not found the try block is run and then finally block.


 


 
public class Xceptions
{
public static void main(String args[]){
try{

System.out.println(1/0);
System.out.println(“Try Block after the error.”); //this line will not print
}
catch(java.lang.ArithmeticException e)
{
System.out.println(“Catch Block”);
System.out.println(“A Stack Trace of the Error:”);
e.printStackTrace();
//e.getMessage(); //This one is useable when we write our own exceptions
System.out.println(“The operation is not possible.”);
}
finally

System.out.println(“Finally Block”);
}
}
}


Previous Home Next