Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Exception Handling Mechanism

  • • From above we knew that when an exception is present in a program, it normally disrupts the normal flow of the application So it is a disadvantages fact.
  • • So java provides a mechanism to handle exceptions known as exception handling mechanism.
  • • The mechanism includes five keywords such as try, catch, finally, throw, and throws.
  • • Basically an exception will present within a try block. When try block is executed, JVM will encounter the catch block. Then the type of exception in try block and type of exception passed as argument in catch block must be match. Then exception will handle otherwise it cannot handle.
  • The general form of try block is:

    Example
    try
    {
    	-	- -  -  -  -//Risky Code
    	-	-  - - - - -
    } 

    The general form of catch block is

    Example
    catch( exception_type e)
    {
    	-	- - - - - -//Handling Code
    	-	- - - - - -
    s}
  • • The try block may contain one or more than one statements that will generate an exception. If anyone statement generate an exception, the remaining statements in the block are skipped and the execution will go to the catch block.
  • • The catch block also may contain one or more than one statements. When catch block will be successfully executed then we will sure our exception will handle.
  • • The catch block is looking like a method definition and passed a single parameter which is a reference to the exception object.
  • • If the catch parameter matches with the type of exception object then the exception is caught and the statement in the catch block will be executed.

Let us see a program to understand the use of try and catch

Example
class ExceptionHandlingDemo
{
	public static void main(String args[])
	{
		int x=0;
		int y=25;
		int z;
		try
		{
			z= y/x;
		}
		catch( ArithmeticException e)
		{
			system.out.println(“vivtech”);
		}
		system.out.println(“viswass”);
	}
}
Output:

vivtech
viswass

Note:

The JVM firstly checks whether the exception is handled or not. If exception is not handled, JVM provides a default exception handler that performs the following tasks:

  • • Prints out exception description.
  • • Prints the stack trace (Hierarchy of methods where the exception occurred).
  • • Causes the program to terminate.

viswass
But if exception is handled by the application programmer, normal flow of the application is maintained i.e. rest of the code is executed.

Multiple Catch

It is possible to write more than one catch block in a program. The general form is as follows

Example:
try
{
	Statement;
}
catch( exception-type1 e)
{
	Statement;
}
catch( exception-type2 e)
{
	Statement;
}
	_ _ _ _ _ _ _
	_ _ _ _ _ _ _

catch( exception-typeN e)
{
	Statement;
}
	_ _  _ _ _ _ _
	_ _ _ _ _ _ _ 

When an exception is found in try a block java encounters multiple catch blocks like cases in switch statement that means the first statement whose parameter will match with the exception object will be executed and remaining statements will be skipped.


Let us see a program to understand this concept

Example:
class MultipleCatchDemo
{
	public static void main( String args[ ])
	{
		int x[ ]= {1,2,3,4};
		int y=2;
		try
		{
			int z=x[4]/y+x[2];
		}
		catch( ArithmeticException e)
		{
			System.out.println(“Silan Entertainment”);
		}
		catch( ArrayIndexOutOfBoundsException e)
		{
			System.out.println(“Silan Software”);
		}
		catch( ArrayStoreExceotion e)
		{
			System.out.println(“silan technologies”);
		}            
		System.out.println(x[1]/x[0]); 
		System.out.println(“silantutorial means Java and Java means silantutorial”);
	}
}

Output:
Silan Software
2
silantutorial means Java and Java means silantutorial

Note:

At a time only one Exception is occurred and at a time only one catch block is executed.


Java Nested try block

The try block within a try block is known as nested try block in java. Sometimes a situation may arise where a part of a block may cause one error and the entire block itself may cause another error. In such cases, exception handlers have to be nested.

Example
Syntax
.... 
try
{
	statement 1;
	statement 2
	try
	{ 
		statement 1;
		statement 2;
	}
	catch(Exception e)
	{
	}
}
catch(Exception e)  
{ 
} 
....

Let's see a simple example of java nested try block.

class NestedDemo
{ 
	public static void main(String args[])
	{
		try
		{ 
			try
			{
				int b =5/0; 
			}
			catch(ArithmeticException e)
			{
				System.out.println(e);
			}
			try
			{ 
				int a[]=new int[5];
				a[5]=15; 
			}
			catch(ArrayIndexOutOfBoundsException e)    
			{
				System.out.println(e);
			}
			System.out.println("other statement);  
		}
		catch(Exception e)
		{
			System.out.println("handeled");
		}
		System.out.println("normal flow");
	}
}

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext