Pr.Pg Next Pg

Exception Handling tutorials

  • An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.

  • The process of responding to the occurrence, during computation, of exceptions anomalous or exceptional events requiring special processing often changing the normal flow of program execution.

  • When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred.

  • Creating an exception object and handing it to the runtime system is called throwing an exception.

  • In servlet application when exception occurs in servlet the web container search for a configuration in web.xml for which type of exception to thrown.

  • You would have to use the error-page element in web.xml to specify the invocation of servlets in response to certain exceptions or HTTP status codes.

 

Exception Handling using try.. catch and finally block

  1. try

  • The first step in constructing an exception handler is to enclose the code that might throw an exception within a try block.

  • In general, a try block looks like the following:

try

{

//Servlet code

}

  • If an exception occurs within the try block, that exception is handled by an exception handler associated with it. To associate an exception handler with a try block, you must put a catch block after it.

 

  1. catch

  • At least one catch is required for the try-catch. finally is not required but that has it own uses.

  • When an exception is throw and a try-catch is found, Java will look through all the catch statments to see what

  • they actually will "catch". This goes in the order that they are written.

  • In general, a try..catch block looks like the following:

try

{

//Servlet Code

}

catch(Exceptiontype name)

{

}

catch(Exceptiontype name)

{

}

  • Each catch block is an exception handler and handles the type of exception indicated by its argument.

  • The argument type, ExceptionType, declares the type of exception that the handler can handle and must be the name of a class that inherits from the Throwable class. The handler can refer to the exception with name.

  • The catch block contains code that is executed if and when the exception handler is invoked. The runtime system invokes the exception handler when the handler is the first one in the call stack whoseExceptionType matches the type of the exception thrown.

  • The system considers it a match if the thrown object can legally be assigned to the exception handler's argument.

 

  1. finally

  • The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs.

  • But finally is useful for more than just exception handlingit allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break.

  • Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

  • In general, a try..catch..finally block looks like the following:

try

{

//Servlet Code

}

catch(Exceptiontype name)

{

}

catch(Exceptiontype name)

}

}

..

..

finally

{

//finally code.

}

 

Different between Error and Exception

Sr No

Error

Exception

1

An error is an irrecoverable condition occurring at runtime.

Exceptions are conditions that occur because of bad input.

2

Such as Out Of Memory error. These JVM errors and you can not repair them at runtime.

FileNotFoundException will be thrown if the specified file does not exist. Or a NullPointerException will take place if you try using a null reference.

3

Though error can be caught in catch block but the execution of application will come to a halt and is not recoverable.

In most of the cases it is possible to recover from an exception probably by giving user a feedback for entering proper values etc.

4

Error in Java are unchecked

Exception in Java are checked and unchecked.

Pr.Pg border                                              Next Pg