Java try-catch


Java try Block

It is one of the block in which we write the block of statements which causes executions at run time in other words try block always contains problematic statements.

Important points about try block

  • If any exception occurs in try block then CPU controls comes out to the try block and executes appropriate catch block.
  • After executing appropriate catch block, even through we use run time statement, CPU control never goes to try block to execute the rest of the statements.
  • Each and every try block must be immediately followed by catch block that is no intermediate statements are allowed between try and catch block.

Syntax of java try-catch

 try
 {
 .......
 }
 /* Here no other statements are allowed 
 between try and catch block */
 catch()
 {
} 
					
  • Each and every try block must contains at least one catch block. But it is highly recommended to write multiple catch blocks for generating multiple user friendly error messages.
  • One try block can contains another try block that is nested or inner try block can be possible.
 try { ....... try { ....... } }
 

Syntax of try-finally block

  • try{
  • //code that may throw exception
  • }finally{}

Java catch block

Java catch block is used to handle the Exception. It must be used after the try block only.

You can use multiple catch block with a single try.

Important points about catch block

  • Catch block will execute provided or exception occurs in try block.
  • It is highly recommended to write multiple catch blocks for generating multiple user friendly error messages to make our application strong.
  • At any point of time only one catch block will execute out of multiple catch blocks.
  • In programmatically in the catch block as a Java programmer we declare an object of sub class and it will be internally referenced by JVM.

Problem without exception handling

Let's try to understand the problem if we don't use try-catch block.

  • public class Testtrycatch1{
  • public static void main(String args[]){
  • int data=50/0;//may throw exception
  • System.out.println("rest of the code...");
  • }
  • }

Output:

Exception in thread main java.lang.ArithmeticException:/ by zero

Solution by exception handling

Let's see the solution of above problem by java try-catch block.

  1. public class Testtrycatch2{
  2. public static void main(String args[]){
  3. try{
  4. int data=50/0;
  5. }catch(ArithmeticException e) { System.out.println(e); }
  6. System.out.println("rest of the code...");
  7. }
  8. }

Output:

Exception in thread main java.lang.ArithmeticException:/ by zero

rest of the code...

Internal working of java try-catch block

exception3

The JVM firstly checks whether the exception is handled or not. If exception is not handled, JVM provides a default exception handler that performs the tasks:

But if exception is handled by the application programmer, normal flow of the application is maintained i.e. rest of the code is executed.