javatutelearn.com

Easy Learning


Home

Search Javatutelearn.com :



Java Exception handling

  • Java provides capability to handle run-time errors using exception handling.
  • Run-time errors/problems/mistakes are called exception.
  • Exception handling uses two statements try and catch to handle the exception.
  • Java try block is used to put those statements in which exceptions are likely to occur.
  • Java catch block is used to handle the exceptions which occurred in try block.
  • Consider the following program:
    class A
    {
    	public static void main(String args[]) 
    	{
    		int a = 0;
    		int b =20;
    		int c = b/a;
    		System.out.println(c);
    	}
    }
    
  • This program shows a logical mistake i.e. an exception that will occur at the time of running the program.
  • The exception in this program is 'divide by zero'. In this situation, compiler terminates the program abnormally.
  • To avoid this abnormal termination by the compiler, it is better to use exception handling mechanism to handle this exception at your end.

Following is the program that uses Exception Handling Mechanism

class A
{
	public static void main(String args[]) 
	{
		try {
				int a = 0;
				int b =20;
				int c = b/a;
				System.out.println(c);
			} 
		catch(ArithmeticException e) 
		{
			System.out.println("divided by zero" + e) ;
		} 

	}
}
  • In this program, the code that may generate exception is put in the try block. Here, exception is 'divide by zero' .
  • When the program runs, it throws an exception named ArithmeticExcetion because of arithmetic expression b/a . Here variable a has the value 0 that causes 'divide by zero' error.
  • The catch block receives this exception and displays a message to user.
  • ArithmeticException is an exception class. The object 'e' of the AirthmeticException class receives the exception.
  • The message is displayed by println() function of the catch block.

Multiple catch example

import java.util.Scanner;
public class demoexcep
{
	public static void main(String args[])
	{
	    try	{
	        Scanner sc = new Scanner(System.in);
	        
			int d, a;
			int c[] = new int[1];
			
			 d = sc.nextInt();
		     a= 10/d;  // Arithmetic Exception if input value of d is 0
			
			c[0]= 1 ;
			c[42]= 100;  // Array index out of Bounds Exception raised
		}
		catch(ArithmeticException e)
		{
			System.out.println("Divide by Zero Error");
		}
		catch(ArrayIndexOutOfBoundsException  e)
		{
			System.out.println("Array index out of Bounds Exception "+e);
		}
			System.out.println("After catch");
	}
 }

Two catch statements with one try statement are used in this program. A try statement may have more than one catch statement.

Nested try Statements example

public class demoexcep {
public static void main(String args[]) {
try {
		int m = args.length;

		System.out.println("m = " + m);
		try { // nested try block

			// In case one command-line arg, then a divide-by-zero exception
			if(m==1) {
			m = m/(m-m); 	// division by zero
			}

			// If two command-line args are used, then out-of-index bounds exception.

			if(m==2) {

			int c[] = { 1 }; // Array is defined with one element

			c[2] = 25; // storing data beyond array indexing, exception will raised
		      } 
			
	    } catch(ArrayIndexOutOfBoundsException e) { // catch of inner try block
		    System.out.println("Array index out-of-bounds: " + e);
		}
	} catch(ArithmeticException e) { // catch of outer try block
		System.out.println("Divide by 0: " + e);
	}
  } //end of main
}// end of class

© Copyright 2016-2024 by javatutelearn.com. All Rights Reserved.