In: Computer Science
Write a try/catch/finally clause to catch one or more specific Exception objects in Java.
Program with its explanation:
| Keyword | Description | 
| Try | Suspicious code that might throws an error and stops the program execution should be written in try block | 
| Catch | Exceptions occured in the code are handled by catch blocks; as the name suggests it catches the exceptions | 
| Finally | Important code/information in our program that needs to be executed irrespective of whether or not the exception is thrown. | 
| Throw | it throw exception explicitly | 
| Throws | it does not throw an exception but is used to declare exceptions. | 
Multiple programs are listed so that you can understand the concept :
Program 1:
public class TryCatchFinally {
   public static void main(String[] args) {
       try
       {
       System.out.println("try
block");
       }
       catch (Exception e)
       {
       System.out.println("catch
block");
       }
       finally
       {
       System.out.println("finally
block");
       }
   }
}
Ouput:
try block
finally block
Explanation: Since there is no suspicious code in try block so catch block doesnt comes into picture.
Program 2:
public class TryCatchFinally {
   public static void main(String[] args) {
       int num1, num2;
       int div;
   try {
  
       Scanner sc =new
Scanner(System.in);
       System.out.println("enter
num1");
       num1=sc.nextInt();
       System.out.println("enter
num2");
       num2=sc.nextInt();
  
       System.out.println("num1/num2
is="+num1+"/"+num2);
   div = num1 / num2; // anything divided by zero is
infinite so it is going to throw exception
   System.out.println("result is="+div);
   System.out.println("end of try block");
   }
   catch (ArithmeticException e) {
   // blocks only Arithmetic exception occurs in try
block
   System.out.println("ArithmeticException Occurred !!
You should not divide a number by zero");
   }
   catch (Exception e) {
   // This is a generic Exception handler
   System.out.println("generic Exception occurred
!!");
   }
   finally
           {
          
System.out.println("finally block");
           }
   }
}
Output:
enter num1
10
enter num2
0
num1/num2 is=10/0
ArithmeticException Occurred !! You should not divide a number by
zero
finally block
Program3.1:
public class TryCatchFinally {
   public static void main(String[] args) {
  
           try{
           int a[]=new
int[7]; // array of 7 values 0 to 6
           a[1]=10/0;//
using second index i.e. 1 and divide by zero throws Arithematic
exception
          
System.out.println("First print statement in try block");
           }
          
catch(ArithmeticException e){
          
System.out.println("Warning: ArithmeticException");
           }
          
catch(ArrayIndexOutOfBoundsException e){
          
System.out.println("Warning:
ArrayIndexOutOfBoundsException");
           }
           catch(Exception
e){
          
System.out.println("Warning: Some Other exception");
           }  
       
          
   finally
           {
          
System.out.println("finally block");
           }
   }
}
Output:
Warning: ArithmeticException
finally block
Program3.2:
import java.io.IOException;
import java.util.Scanner;
import javax.imageio.IIOException;
public class TryCatchFinally {
   public static void main(String[] args) {
  
           try{
           int a[]=new
int[7]; // array of 7 values 0 to 6
           a[9]=10/2;//
using 10th index i.e. 9 and throws ArrayIndexOutOfBoundsException
bcz array if of 7 // values
          
System.out.println("First print statement in try block");
           }
          
catch(ArithmeticException e){
          
System.out.println("Warning: ArithmeticException");
           }
          
catch(ArrayIndexOutOfBoundsException e){
          
System.out.println("Warning:
ArrayIndexOutOfBoundsException");
           }
           catch(Exception
e){
          
System.out.println("Warning: Some Other exception");
           }  
       
          
   finally
           {
          
System.out.println("finally block");
           }
   }
}
Output:
Warning: ArrayIndexOutOfBoundsException
finally block
Program4:
public class TryCatchFinally {
   public static void main(String[] args) {
  
       try
       {
       System.out.println("try
block");
         
       throw new
NullPointerException("null occurred");
       }
       catch (NumberFormatException
e)
       {
       System.out.println("catch block
1");
       }
       catch (NullPointerException
e)
       {
       System.out.println("catch block
2");
       }
       catch (Exception e)
       {
       System.out.println("catch block
3");
       }
       finally
       {
       System.out.println("finally
block");
       }
   }
}
Output:
try block
catch block 2
finally block
Explanation: catch block 2 because specific exception was mentioned i.e. nullpointerexception and it goes to specific block. If you comment out Catch2 block then it is going to print
try block
catch block 3
finally block
as generic exception block then catches the exception and prints the code inside it.
Program5:
public class TryCatchFinally {
       public static void main(String[]
args) {
      
           try
           {
          
System.out.println("try block");
          
  
           throw new
NullPointerException("Null occurred");
           }
           finally
           {
          
System.out.println("finally block");
           }
       }
   }
Output:
try block
finally block
Exception in thread "main" java.lang.NullPointerException: Null
occurred
   at
Threadds.TryCatchFinally.main(TryCatchFinally.java:18)
Explanation: SInce no catch block is there to handle the exception so it is going to throw the exception.
Program6:
public class TryCatchFinally {
   void testMethod(int num) throws IOException,
ArithmeticException{
   if(num==1)
   throw new IOException("IOException Occurred");
   else
   throw new
ArithmeticException("ArithmeticException");
   }
  
   public static void main(String args[]){
   try{
       TryCatchFinally obj=new
TryCatchFinally();
   obj.testMethod(1);
   }catch(Exception ex){
   System.out.println(ex);
   }
   }
   }
Output:
java.io.IOException: IOException Occurred
Explanation: If   obj.testMethod(2);
is written it gives output
as  java.lang.ArithmeticException:
ArithmeticException
as it is defining exceptions explicitly.
Program7:
public class TryCatchFinally {
   public static void main(String[] args) {
       int num1, num2;
       int div;
   try {
  
       Scanner sc =new
Scanner(System.in);
       System.out.println("enter
num1");
       num1=sc.nextInt();
       System.out.println("enter
num2");
       num2=sc.nextInt();
  
       System.out.println("num1/num2
is="+num1+"/"+num2);
   div = num1 / num2;
   System.out.println("result is="+div);
   System.out.println("end of try block");
   }
   catch (ArithmeticException e) {
   System.out.println("You should not divide a number by
zero");
   e.printStackTrace();
   }
   catch (Exception e) {
   System.out.println("Exception occurred");
   e.printStackTrace();
   }
   finally
           {
          
System.out.println("finally block");
           }
   }
}
Output:
enter num1
10
enter num2
0
num1/num2 is=10/0
You should not divide a number by zero
finally block
java.lang.ArithmeticException: / by zero
   at
Threadds.TryCatchFinally.main(TryCatchFinally.java:23)
Explanation: e.printStackTrace(); is a method of Java’s throwable class which prints the throwable along with other details like the line number and class name where the exception occurred.
NOTE: Please add necessary import statements to execute the program smoothly.