❮ Previous Next ❯

Exception Handling Mechanism

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:

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(“Java8s means Java and Java means Java8s”);
}
}

Output:


Silan Software
2
Java8s means Java and Java means Java8s

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");
}
}

❮ Previous Next ❯


  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved