Exception Handling - Java

A Java exception is an object that describes an exceptional(error) condition that has occurred in a peace of code . when an exceptional condition arrises , an object represendting that exception is created and thrown in the method that caused the error. That method may choose to handle the exception itself, or pass it on, either way at some point the exception is caught and processed, exceptions can be generated by the java runtime system, or they can be manully generated by your code.

                        Java exception handling is managed by five keywords. They are try, catch, throw, throws and finally.
Program statements that you want to monitor for exception are contained with in a try block.
If an exception occurs with in the try block, it is thrown. Your code can catch this exception using catch handle it in some rational manner. System generated exception area automatically thrown by the java run time system. To manually throw an exception use the keyword a throw.
Any exception that is thrown out of a method must be specified as such by a throws class.
Any code that absolutely must be executed before a method returns is put in a finally block.

The general form of the try

try
{
block of code to monitor for errors
}
catch(exceptionclass obj)
{
statements;
}
catch(exceptionclass obj)
{
statements;
}
finally
{
statements;
}

 

WRITE A PROGRAM ON TRY CATCH BLOCKS.

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

        }

}         

WRITE A PROGRAM ON MULTIPLE CATCH BLOCKS.

class Sample
{
public static void main(String args[])
{
try
{
int a;
a=args.length;
System.out.println("The value of a: "+a);
int b=20/a;
int c[]={1};
c[40]=80;
}
catch(ArithmeticException e)
{
System.out.println("Div by Zero");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array Out of Limit");
}
finally
{
System.out.println(“This is finally block……”);
}
}
}

WRITE A PROGRAM ON EXCEPTION CLASS

class Sample
{
public static void main(String args[])
{
try
{
int c[]={1};
c[40]=80;
}
catch(Exception e)
{
System.out.println("Unknown Exception: "+e);
}
}
}

WRITE A PROGRAM ON NESTED TRY STATEMENTS.

class Sample
{
public static void main(String args[])
{
try
{
int a=args.length;
int b=40/a;
try
{
if(a==1)
{
a=a/(a-a);
}
if(a==2)
{
int c[]={10,20};
c[10]=5;
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
}
catch(ArithmeticException e)
{
System.out.println("Divided by zero");
}
}
}

Note:-
                        If there is no catch block for raising exception inner try block control will search the outer catch statements for the raised exception.

WRITE A PROGRAM ON NESTED TRY STATEMENTS USING ANOTHER METHOD.

class Sample
{
static void get(int x)
{
try
{
if(x==1)
{
x=x/(x-x);
}
if(x==2)
{
int c[]={10,20};
c[10]=30;
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
}
public static void main(String args[])
{
try
{
int a=args.length;
int b=20/a;
get(a);
}
catch(ArithmeticException e)
{
System.out.println(e);
}
}
}

Handling More Than One Type of Exception

In Java SE 7 and later, a single catch block can handle more than one type of exception. This feature can reduce code duplication.
Consider the following example, which contains duplicate code in each of the catch blocks:

catch (IOException e) {
     System.out.println(e);
catch (SQLException e) {
     System.out.println(e);
 
 
}
 

In releases prior to Java SE 7, it is difficult to create a common method to eliminate the duplicated code because the variable e has different types.

 

The catch clause specifies the types of exceptions that the block can handle, and each exception type is separated with a vertical bar (|) Pipe Symbol.
.
Note: If a catch block handles more than one exception type, then the catch parameter is implicitly final

Programe on multiple exceptions in Single catch block

class Sample
{
public static void main(String args[])
{
try
{
int a;
a=args.length;
System.out.println("The value of a: "+a);
int b=20/a;
int c[]={1};
c[40]=80;
}
catch(ArithmeticException | ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
}
}

 

WRITE A PROGRAM ON THROW

class Sample
{
static void get()
{
try
{
throw new NullPointerException("Demo");
}
catch(NullPointerException e)
{
System.out.println("inside of get");
throw(e);
}
}
public static void main(String args[])
{
try
{
get();
}
catch(NullPointerException e)
{
System.out.println("recaught"+e);
}
}
}

Throws:-
                        If a method is capable of causing an exception that doesn’t handle it must specify its behavior. A throws clause lists the types of exception that a method might throw. This is necessary for all exceptions except RuntimeException or any of their sub classes.

Syntax:-
                        type methodname(parameter list) throws exceptionlist
{
Statements;
}

WRITE A PROGRAM ON THROWS.

class Sample
{
static void get() throws IllegalAccessException
{
throw new IllegalAccessException("Hello");
}
public static void main(String args[])
{
try
{
get();
}
catch(IllegalAccessException e)
{
System.out.println("Caught"+e);
}
}
}

WRITE A PROGRAM ON CUSTOM EXCEPTION.

class MyException extends Exception
{
int a;
MyException(int x)
{
a=x;
}
public String toString()
{
return("my Exception["+a+"]");
}
}
class Sample
{
static void compute(int x) throws MyException
{
if(x>10)
throw new MyException(x);
System.out.println("Normal Exits");
}
public static void main(String args[])
{
try
{
compute(5);
compute(20);
}
catch(MyException e)
{
System.out.println("Caught"+e);
}
}
}

 

For
Online Classes

Contact Us: 9885348743