MY mENU


Friday 10 February 2012

Try and Catch Blocks in Exception handling


Try() Block:

       The first step in constructing an Exception Handler is to enclose the code that might throw an Exception within a try block.

Try Block Syntax :

try {
code
}
catch and finally blocks . . .

- The labelled code in above example contains one or more legal lines of code that could throw an Exception. So enclose the Exception-throwing statements within a try block.

- We can put each line of code that might throw an Exception within its own try block and provide separate Exception Handlers (Catch Block) for each. Or, we can put all the code within a single try block and associate multiple handlers with it.

- 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.

Catch() Block:

          We associate Exception Handlers with a try block by providing one or more catch blocks directly after the try block. No code can be between the end of the try block and the beginning of the first catch block.

When an exception is raised in the try block and it throws it to the catch clause and catch clause will handle it.

Syntax :

try {

} 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 whose ExceptionType 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.

Exception Handlers can do more than just print error messages or halt the program. They can do error recovery, prompt the user to make a decision, or propagate the error up to a higher-level handler using Chained Exceptions.


Finally Block():
    
           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 Handling — it 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.

- The Runtime System always executes the statements within the Finally Block regardless of what happens within the try block. So it's the perfect place to perform cleanup. TheFinally Block is a key tool for preventing resource leaks. When closing a file or otherwise recovering resources, place the code in a Finally Block to insure that resource is always recovered.

Note : If the JVM exits while the try or catch code is being executed, then the Finally Block will not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the Finally Block will not execute even though the application as a whole continues.


Finally block is executed after try-catch block.

in this block we have to save any unsaved information before termination of

the program.

we can close any database connections exists.

Finally is also a block, where code gets executed. and it is possible to occurs Exception there. and if any Exception occurs in finally result is Same Termination of the Program.
Solution : Use try Catch blocks there to avoid termination.

Eg:



public class ExceptionTest {

public static void main(String args[])
{
try
{
System.out.println("Inside Try");
}catch(Exception e)
{
System.out.println("Inside catch");
}
finally
{
try
{
int i=10/0;
}catch(Exception e)
{
System.out.println(e);
}
}
}

}

exception may arise in finally block . To avoid automatic termination we can use try catch inside finally .....


No comments:

Post a Comment