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)

C:\Users\Raj\Desktop>
Example : A simple (as above) java program with Exception Handling.
import java.io.*;
import java.util.Scanner;

class Exception
 {
  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) 
     {
       System.out.println("Exception arise & caught is : Division by zero.");
     }
  }
}

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
12
0
Exception arise & caught is : Division by zero.

C:\Users\Raj\Desktop>
Example : A simple (as above) java program without/with 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[7]);
	   System.out.println("Value Stored at : " + arr[2]);		
	}
}
F:\>javac abc1.java
F:\>java Example
Value Stored at : 50
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
        at Example.main(abc1.java:9)

--------  OR  --------
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

 365 total views,  1 views today

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.