1.

How are exceptions handled in Java? Explain with the help of a suitable example.

Answer»

An error situation that is unexpected in the program execution and causes it to terminate unexpectedly is called an exception.

Java provides the following keywords to handle an exception:

try - A try block surrounds the part of the code that can generate exception(s).

catch – The catch blocks follow a try block. A catch block contains the exception handler - specific code that is executed when the exception occurs.

The optional finally block is always executed when the try block exits.

For example Division by zero exception:

try 

{

int quotient = divide(10,0); 

System. out. print ln (quotient);

catch (Exception e) 

System. out. print ln(e. get Message()); 

}



Discussion

No Comment Found

Related InterviewSolutions