Saturday, May 30, 2009

[JAVA] Exception Handling

Exception Handling :- The purpose of exception handling mechanism is to detect and report that an exception is occurred so that appropriate action can be taken. There are four phase in entire exception handling process –

1) Find the problem (Hit the exception)

2) Inform that an error has occurred (Throw the exception)

3) Receive the error information (Catch the exception)

4) Take necessary actions (Handle the exception)


Syntax of Exception Handling -
Try

{

Statement; //generates an exception

}

Catch

{

Statement; //process the exception

}



Example –

Class SimpleDivision(){

Private static void Main(){

int a=2, b=0, c=6;

try{

c=a/b;

system.out.println(c);

system.out.println(“Successful.”);

}

Catch(Arithmatic Exception e){

system.out.println(“Not successful.”);

}

}

}


[ OUTPUT : Not Successful. ]

No comments:

Post a Comment