Throw, Try and Catch Blocks:
To respond to an exception, the call to the method which produces it must be placed inside a try block. The try block is a block of code starting along with the try keyword followed through a left and a right curly brace. Each try block is related along with one or more catch blocks. Here is a try block:
try
{
// method calls go here
}
If a method is to catch exceptions thrown through the methods it calls, the calls must be placed inside a try block. It is handled in a catch block if an exception is thrown. Various catch blocks handle various types of exceptions. The given are a try block and a catch block set up to handle exceptions of Exception type:
try
{
// method calls go here
}
catch( Exception e )
{
// handle exceptions here
}
Whenever any method in the try block throws any type of exception, execution of the try block ceases. A Program control passes instantly to the related catch block. It takes over if the catch block can handle the given exception type. If it cannot handle the exception, the exception is passed to the method's caller. Within an application, that process goes on until a catch block catches the exception or the exception reaches the main () function uncaught and causes the application to terminate.