A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

Try Catch

Try Catch

How to handle the Exceptions?

To handle the Exceptions We use some keywords i.e.

  • try

  • catch

  • throw

  • throws

  • finally

Try

  • It is a a block here we place the code where exception can occurs .

  • A try block must be followed by catch or finally or can be both.

  • We cannot use try block alone.

  • A single try block can have multiple catch blocks.

Syntax-





try
{
 //statements to be executed..
}


 

program 1 without using try block:-





class Test1
{
 public static void main(String args[]) 
 {
        int num1 = 0;
        int num2 = 20 / num1;
        System.out.println(num2);
        System.out.println("Rest of the code...");
    }
}

OutPut:-
Exception in thread "main" java.lang.ArithmeticException: / by zero
        at Test1.main(TryProgram.java:6  


 

Catch

  • The catch must be associated with a try block.

  • In the catch block we handle the exceptions.

  • A single try block can have multiple catch blocks.

  • We cannot use catch block alone it must be use with try block.

  • We use try and catch block to handle the arising exceptions.

Syntax:-With single catch block





try
{
 //statements to be executed..
}
catch(Name of Exception)
{
 //code of handled exception
}


 

Syntax:-multiple catch block





try 
{
    //statements to be executed..
}
catch(ArrayIndexOutOfBoundsException e) 
{
    //code of handled exception
}
catch(NullPointerException e)
{
    //code of handled exception
}
catch(Exception e)
{
    //code of handled exception
}


 

program 2 by using try catch block:-





class Test1
{
 public static void main(String args[]) 
 {
  try 
  {
   /*This statement is suspectable the code can throw exception
    *we handled this block statement by placing these statements
   *inside try block and handled the exception in catch block*/
   
   int num1 = 0;
   int num2 = 20 / num1;
   System.out.println(num2);
   System.out.println("Rest of the code...");
  }

  catch (ArithmeticException e) 
  { 
/* This block will only execute to handle Arithmetic exception that will occurs in tryblock.*/
   
    System.out.println("We can not divide any number by zero..");
  }
 }
}

OutPut:-
We can not divide any number by zero


 

program 2 by using try and multicatch block:-





class Test1
{
 public static void main(String args[]) 
 {
  try 
  {
    /*This statement is suspectable the code can throw exception
    *we handled this block statement by placing these statements
    *inside try block and handled the exception in catch block*/
    
   int num1 = 0;
   int num2 = 20 / num1;
   System.out.println(num2);
   System.out.println("Rest of the try code...");
  }

  catch (ArithmeticException e) 
  { 
    
    /* This block will only execute to handle Arithmetic exception that will occurs in try block.*/
    
    System.out.println("We can not divide any number by zero..");
  }
  
  
  catch (Exception e) 
  {
   
   /* This is a parent Exception of all the Exceptions which means it can handle
   all the exceptions. This will execute if the upper catch block cannit handle the exception.*/
   
   
   System.out.println("Exception Handled");
  }
  System.out.println("Rest of Without Exception Code...");
 }
}

OutPut:-
We can not divide any number by zero..
Without Exception Code...


 

LEARN TUTORIALS

.

.