Exception Handling

PREVIOUS



Exceptions



·       In Exception is an abnormal condition which occurs during the execution of a program with the help of its well-defined exception handling mechanism..



·       Exceptions are erroneous events like division by zero, opening of a file which does not exists,etc.



Java handles Exception using five keywords.



try, catch ,finally, throws and throw

       

The Object class has subclass called Throwable to handle Exceptions and errors.



Object -> Throwable

                             -> Error

                            -> Exception

                            -> Runtime Exception

                            -> IOException



Pre-Defined Exception



        ClassNotFoundException

        ArithmeticException

        ArrayIndexOutOfBoundsException

        NullPointerException

        NumberFormatException



Default Exception handling



When a program throws an exception, it comes to a stop. This exception should be caught by an exception handler and dealt with immediately. In case of provision of an Exception handler, the handling is done by handler. Suppose there are no handlers, the Java Runtime System provides default handlers. This displays a string which describes the Exception and the Point.





try and catch



The code sequence, which is to be guarded, should be placed inside the try block. The catch clause should immediately follow the try block. The catch clause can have statements explaining the cause of the exception generated.



Syntax



try

{

 <statements>

}catch(Exception )

 {

  <statements>

 }













class excep

{

int i=10;



void view()

{

try

{

System.out.println(i+10);

}catch(ArithmeticException e)



{

System.out.println("some error");

}



}



public static void main(String ar[])

{

System.out.println("this is rose");

excep e=new excep();

e.view();

System.out.println("After Excep");

}

}



throws



Without using the try block we can handle the throws class.

Only find out the error’s, can’t access the after exception statements.



Syntax



<method> throws <Exception name>



program:



class throws

{

public static void main(Strings ar[])throws  ArithmeticException

{

int num=0;

System.out.println(“num”+num/0);

//System.out.println(“hai”);

}

}





finally



When an exception is generated in a program, sometimes it may be necessary to perform certain activities before termination. In such cases, the try block apart from the catch clause, also has finally block within such activities can be performed. This block is executed after the execution of statements within the try/catch block.



Syntax



finally

{

 <statements>

}







program  finally



class finally1

{

public static void main(String ar[])

{

        try

        {

        int a=Integer.parseInt(ar[0]);

        int b=Integer.parseInt(ar[1]);

        System.out.println("modulus"+a%b);

        }

        catch(ArithmeticException e)

        {

        System.out.println("error in denominator");

        }

        catch(ArrayIndexOutOfBoundsException e)

        {

        System.out.println("error in index value");

         }



        catch(NumberFormatException n)

        {

        System.out.println("data type error");

        }

  finally

   {

    System.out.println("finally block");

    }

}

}





throw



The throw statement is used to explicitly throw an exception. First a handle on an instance of Throwable must be got via a parameter into a catch clause or by creating one using the new operator.





import java.io.*;

class poo

{

public static void main(String ar[])throws IOException

{

DataInputStream d=new DataInputStream(System.in);

int a=Integer.parseInt(d.readLine());

System.out.println("this is a val:  "+a);

throw new NullPointerException("hello");

}

}







User-Defined Exception



Customized exceptions are necessary to handle abnormal condition of applications created by the user.



The user-defined exception can be created by deriving a class from the Exception class.



Syntax



class <name> extends Exception







// MyExeption is a User-defined Exception

import java.lang.Exception;

class myex extends Exception

{

myex(String m)

{

super(m);

}

}

class usrex

{

public static void main(String a[])

{

int x=3,y=1000;

try

{

double z=x/y;

if(z<0.01)

{

throw new myex("number is too small");

}

else

{

throw new myex("number is too high");

}

}

catch(myex e)

{

System.out.println("caught my exception");

System.out.println(e.getMessage());

}

finally

{

System.out.println("welcome");

}

}

}

No comments:

Post a Comment