Example : A simple Java program without Exception Handling.
import java.io.*;
import java.util.Scanner;
 
class Exception 
{
  public static void main(String[] args) 
  { 
    int x, y, output;       
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter two integer number");
 
    x = sc.nextInt();
    y = sc.nextInt(); 
    output = x / y;       
    System.out.println("Result= " + output);
  }
}

Save as je.java on desktop
Output1 : 
C:\Users\Raj>cd desktop

C:\Users\Raj\Desktop>javac je.java

C:\Users\Raj\Desktop>java Exception
Enter two integer number
21
3
Result= 7

Output2 : 
C:\Users\Raj\Desktop>javac je.java

C:\Users\Raj\Desktop>java Exception
Enter two integer number
20
0
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Exception.main(je.java:17)
Example : A Java program to display Exception Handling using Try, Catch, and Finally Statement.
import java.io.*;
import java.util.Scanner;

class Test
 {
  public static void main(String[] args) 
  { 
    int x, y, output;
 
    Scanner input = new Scanner(System.in);
    System.out.println("Enter two integer number");
 
    x = input.nextInt();
    y = input.nextInt();
 
    // try block
     try 
     {
       output  = x / y;
       System.out.println("Result = " + output);
     }
 
     // catch block 
     catch (ArithmeticException e)   //Specific Exception
     {
       //System.out.println("Exception arise & caught is : Division by zero.");
       System.out.println("Exception arise is = " +e.getMessage());
     }
     finally
     {
       System.out.println("Program finally Terminated");
     }
  }
}

Output:
D:\>java Test
Enter two integer number
20
5
Result = 4
Program finally Terminated
Example : A simple Java program demonstration with Specific Exception Handling.
import java.io.*;
import java.util.Scanner;

class Test
 {
  public static void main(String[] args) 
  {
 
    int x, y, output;
 
    Scanner input = new Scanner(System.in);
    System.out.println("Enter two integer number");
 
    x = input.nextInt();
    y = input.nextInt();
 
    // try block
     try 
     {
       output  = x / y;
       System.out.println("Result = " + output);
     }
 
     // catch block 
     catch (ArithmeticException e)   //Specific Exception
     {
       //System.out.println("Exception arise & caught is : Division by zero.");
       System.out.println("Exception arise is = " +e.getMessage());
     }
  }
}

Output1 : 
C:\Users\Raj\Desktop>javac je1.java

C:\Users\Raj\Desktop>java Exception
Enter two integer number
25
5
Result = 5

Output2 : 
C:\Users\Raj\Desktop>javac je1.java

C:\Users\Raj\Desktop>java Exception
Enter two integer number
20
0
Exception arise is = Division / by zero
Example : A simple Java program without/with Specific Exception Handling.
(Without Exception Handling)

class Example
{
	public static void main(String[] args)
	{	
	   int arr[] = {41,85,72,10,50};
	   System.out.println("Value Stored at : " + arr[4]);	   
	   System.out.println("Value Stored at : " + arr[2]);
           System.out.println("Value Stored at : " + arr[7]);		
	}
}

F:\>javac abc1.java
F:\>java Example

Value Stored at : 50
Value Stored at : 72
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
        at Test.main(Program.java:8)

--------  OR  --------

(With Specific Exception Handling)

class Example
{
	public static void main(String[] args)
	{
		try
		{
		   int arr[] = {41,85,72,10,50};

		   System.out.println("Value Stored at : " + arr[4]);
		   System.out.println("Value Stored at : " + arr[7]);
                   System.out.println("Value Stored at : " + arr[2]);
		}
		catch( ArrayIndexOutOfBoundsException e)
		{
		   System.out.println("Value is not present because it is beyond the 
                   size of array");
		}
	}
}

Output :

F:\>javac abc1.java
F:\>java Example
Value Stored at : 50
Value is not present because it is beyond the size of array
Example : A Java program to demonstrate  Multiple Catch Statements using Exception Handling.
class Sample 
{  
    public static void main(String[] args)
    {  
          
       try
         {    
            int x[]=new int[10];    
                x[12]=45/0;    
         }    
       catch(ArithmeticException e)  
         {  
            System.out.println("Division by Zero Exception occurs");  
         }    
       catch(ArrayIndexOutOfBoundsException e)  
         {  
            System.out.println("Array Index Out Of Bounds Exception occurs");  
         }    
       catch(Exception e)  
         {  
            System.out.println("An Exception occurs");  
         }             
     }  
} 

Output:
D:\>javac Program.java

D:\>java Sample
Division by Zero Exception occurs 

---------------------  OR  ----------------------

class Sample 
{  
    public static void main(String[] args)
    {          
       try
         {    
            int x[]=new int[10];                   
                
            System.out.println(x[13]);      
         }    
       catch(ArithmeticException e)  
         {  
            System.out.println("Division by Zero Exception occurs");  
         }    
       catch(ArrayIndexOutOfBoundsException e)  
         {  
            System.out.println("Array Index Out Of Bounds Exception occurs");  
         }    
       catch(Exception e)  
         {  
            System.out.println("An Exception occurs");  
         }             
     }  
}  

Output:
D:\>javac Program.java

D:\>java Sample
Array Index Out Of Bounds Exception occurs

-------------------  OR  -------------------

class Sample 
{  
    public static void main(String[] args)
    {         
       try
         {    
            String str=null;  
            System.out.println(str.length());       
         }    
       catch(NullPointerException e)  
         {  
            System.out.println("Null Pointer Exception occurs");  
         }    
       catch(ArrayIndexOutOfBoundsException e)  
         {  
            System.out.println("Array Index Out Of Bounds Exception occurs");  
         }    
       catch(Exception e)  
         {  
            System.out.println("An Exception occurs");  
         }             
     }  
}  

Output: 
D:\>javac Program.java

D:\>java Sample
Null Pointer Exception occurs

-----------------  OR  -----------------

class Sample 
{  
    public static void main(String[] args)
    {         
       try
         {    
           String str="India";  
	   int i=Integer.parseInt(str);//NumberFormatException         
         }    
       catch(NullPointerException e)  
         {  
            System.out.println("Null Pointer Exception occurs");  
         }    
       catch(ArrayIndexOutOfBoundsException e)  
         {  
            System.out.println("Array Index Out Of Bounds Exception occurs");  
         }    
       catch(NumberFormatException e)  
         {  
            System.out.println("Number Format Exception occurs");  
         }             
     }  
}  

Output: 
D:\>javac Program.java

D:\>java Sample
Number Format Exception occurs
Example : A Java program to demonstrate  Nested Try Statements using Exception Handling.
class First
{    
 public static void main(String args[])
    {   
      //outer try block   
      try
        {    
          //String str=null;
	  //System.out.println(str.length());

         //inner try block 1  
         try
           {                 
             int x = 12/0;    
           }
  
          //catch block of inner try block 1  
         catch(ArithmeticException e)  
           {  
             System.out.println(e);  
           }    
       
    
         //inner try block 2  
         try
           {    
              int arr[]=new int[5];    
  
              //assigning the value out of array bounds  
              arr[6]=40;    
           }  
  
         //catch block of inner try block 2  
          catch(ArrayIndexOutOfBoundsException e)  
           {  
              System.out.println(e);  
           }       
      }
  
      //catch block of outer try block  
      catch(NullPointerException e)  
       {  
          System.out.println("Outer Catch Null Pointer Exception Arise");  
       }     
  
    }    
 }  

Output:
D:\>javac Program.java

D:\>java First
java.lang.ArithmeticException: / by zero
java.lang.ArrayIndexOutOfBoundsException: 6

------------------  OR  -------------------

class First
{    
 public static void main(String args[])
    {   
      //outer try block   
      try
        {    
          String str=null;
	  System.out.println(str.length());

         //inner try block 1  
         try
           {    
             System.out.println("Division by 0 Exception");    
             int x = 12/0;    
           }  
          //catch block of inner try block 1  
         catch(ArithmeticException e)  
           {  
             System.out.println(e);  
           }    
       
    
         //inner try block 2  
         try
           {    
              int arr[]=new int[5];    
  
              //assigning the value out of array bounds  
              arr[6]=40;    
           }  
  
         //catch block of inner try block 2  
          catch(ArrayIndexOutOfBoundsException e)  
           {  
              System.out.println(e);  
           }     
       
      }  
      //catch block of outer try block  
      catch(NullPointerException e)  
       {  
          System.out.println("Outer Catch Null Pointer Exception Arise");  
       } 
    }    
 }  

Output:
D:\>javac Program.java

D:\>java First
Outer Catch Null Pointer Exception Arise
Example : A simple Java program to demonstrate  Throw Keyword using Exception Handling.
class Example 
{
  public static void divideByZero() 
  {
    // throw a customized exception
    throw new ArithmeticException("Trying to divide by 0");
  }

  public static void main(String[] args) 
  {
    divideByZero();
  }
}

Output:
D:\>javac Program.java

D:\>java Test
Exception in thread "main" java.lang.ArithmeticException: Trying to divide by 0
        at Test.divideByZero(Program.java:7)
        at Test.main(Program.java:12)
Example : A Java program to demonstrate  Throws Keyword using Exception Handling.
import java.io.*;

class Example 
{
   // declareing the type of exception
   public static void findFile() throws IOException 
    {
    	// code that may generate IOException
    	File newFile = new File("test.txt");
    	FileInputStream stream = new FileInputStream(newFile);
    }

   public static void main(String[] args) 
   {
     try 
      {
        findFile();
      }
     catch (IOException e) 
      {
        System.out.println(e);
      }
   }
}

Output:
D:\>javac Program.java

D:\>java Example
java.io.FileNotFoundException: test.txt (The system cannot find the file specified)

NB: 
When we run this program, if the file test.txt does not exist, FileInputStream throws a FileNotFoundException which extends the IOException class.
Example : A Java program to demonstrate the Multiple Throws Concept using Exception Handling.
import java.io.*;
class Example
 {
  public static void findFile() throws NullPointerException, IOException, InvalidClassException 
	{
    
    	// codes that may produce NullPointerException 
    		---
		--- 

    	//  codes that may produce IOException
    		---
		---

    	// codes that may produce InvalidClassException 
    		---
		--- 
  	}

  	public static void main(String[] args) 
	{
    	  try
          {
            findFile();
          } 
          catch(IOException e1)
          {
            System.out.println(e1.getMessage());
          } 
          catch(InvalidClassException e2)
          {
            System.out.println(e2.getMessage());
          }
	  catch(NullPointerException e3)
          {
            System.out.println(e3.getMessage());
          }
        }
}
Example : A Java program to demonstrate  Throw Keyword using Throws Exception Handling.
import java.io.*;
class Example
 {
  public static void findFile() throws IOException 
   {
     throw new IOException("File does not exists");
   }

  public static void main(String[] args) 
   {
    try 
     {
      findFile();      
     } 
    catch (IOException e) 
    {
      System.out.println(e.getMessage());
    }
   }
 }

Output:
D:\>javac Program.java

D:\>java Example
File does not exists

Loading

Categories: Java

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.