try catch finally block in java


We can enclose exception prone code in >
  • try-catch block, or
  • try-finally block, or
  • try-catch-finally block.

Using try-catch block
      try{
             //Code to be enclosed in try-catch block
      }catch(Exception e){
      }



Using try-finally block
      try{
             //Code to be enclosed in try-finally block
      }finally{
      }          


Using try-catch-finally block
      try{
             //Code to be enclosed in try-catch-finally block
      }catch(Exception e){
      }finally{
      }



We cannot use try block alone, it must be followed by either catch or finally.
Using only try block will cause compilation error
      try{
             //only try block will cause compilation error
      }




Likewise, we cannot use catch block alone, it always follows try block.
Using only catch block will cause compilation error
      catch{
             //only catch block will cause compilation error
      }




Likewise, we cannot use finally block alone, it always follows try or try-catch block.
Using only finally block will cause compilation error
      finally{
             //only finally block will cause compilation error
      }





How try-catch block helps us >
With try-catch block we can avoid awkward termination of program.

If any exception occurs and it is not handled properly, rest of the code is not executed >
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class ExceptionTest {
   public static void main(String[] args) {
          int i=10/0;   //will throw ArithmeticException
          System.out.println("Did this line execute?");
   }
}
/*OUTPUT
Exception in thread "main" java.lang.ArithmeticException: / by zero
   at javaMadeSoEasy.ExceptionTest.main(ExceptionTest.java:4)
*/
In the above program ArithmeticException was thrown at line 4, it wasn’t handled properly, so rest of the code didn’t executed.


If any exception occurs and it is handled properly, rest of the code gets executed >
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class ExceptionTest {
   public static void main(String[] args) {  
          try{
                 int i=10/0; //will throw ArithmeticException
          }catch(Exception e){
                 System.out.println("Exception handled  properly in catch block");
          }         
          System.out.println("Code after exception handling");
   }
}
/*OUTPUT
Exception handled  properly
Code after exception handling
*/
In the above program ArithmeticException was thrown at line 5, it was handled properly, so rest of the code did executed.



but by handling these kind of exception we avoid such interruptions and end up giving some meaningful message to user.





RELATED LINKS>


EXCEPTIONS - Top 60 interview questions and answers in java for fresher and experienced - detailed explanation with diagrams Set-1 > Q1- Q25


EXCEPTIONS - Top 60 interview questions and answers in java for fresher and experienced - 30 important OUTPUT questions Set-2 > Q26- Q60


eEdit
Must read for you :