Java Program to Handle the Exception Using Try and Multiple Catch Block

This is a Java program to handle the Exception using Try and Multiple Catch Block.

A Java exception is an error condition that has occurred in a piece of code on violation of some rules.
Java Exception Handling is managed via five keywords: try, catch, throw, throws, and finally.

Here is the source code of the Java Program to Handle the Exception Using Try and Multiple Catch Block. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.io.FileInputStream;
  2. import java.util.Scanner;
  3.  
  4. public class Try_Catch 
  5. {
  6.     public static void main(String[] args) 
  7.     {
  8.         int a=5,b=0,c,d,f;
  9.         try
  10.         {
  11.             Scanner s=new Scanner(System.in);
  12.             System.out.print("Enter a:");
  13.             a=s.nextInt();
  14.             System.out.print("Enter b:");
  15.             b=s.nextInt();
  16.             System.out.print("Enter c:");
  17.             c=s.nextInt();
  18.             d=a/b;
  19.             System.out.println(d);
  20.             f=a%c;
  21.             System.out.println(f);
  22.             FileInputStream fis = null;
  23.             /*This constructor FileInputStream(File filename)
  24.             * throws FileNotFoundException which is a checked
  25.             * exception*/
  26.             fis = new FileInputStream("B:/myfile.txt"); 
  27.             int k; 
  28.             /*Method read() of FileInputStream class also throws 
  29.             * a checked exception: IOException*/
  30.             while(( k = fis.read() ) != -1) 
  31.             { 
  32. 		System.out.print((char)k); 
  33.             }    
  34.             /*The method close() closes the file input stream
  35.             * It throws IOException*/
  36.             fis.close(); 	
  37.         }
  38.         catch(IndexOutOfBoundsException e)
  39.         {
  40.             System.out.println(e);
  41.         }
  42.         catch(NullPointerException e)
  43.         {
  44.             System.out.println(e);
  45.         }
  46.         catch(ArithmeticException e)
  47.         {
  48.             System.out.println(e);
  49.         }
  50.         catch(Exception e)
  51.         {
  52.             System.out.println(e);
  53.         }
  54.     }
  55. }

Output:

$ javac Try_Catch.java
$ java Try_Catch
 
Enter a:4
Enter b:5
Enter c:6
0
4
java.io.FileNotFoundException: B:/myfile.txt (No such file or directory)

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.