Try Catch Implementation


Syntax :-
 
try
{
          Code…
}
catch[Exception Class object]
{
          Code…
}
.
.
.
Finally
{
          Code…
}
  • A try block can be followed by any number of catch blocks, writing finally block is optional
  • Writing an exception class with in the catch block is optional
  • If a catch block is used without an exception class then it is known as generic catch block
  • If catch block is used with an exception class then it is known as specific catch block

Try: This block contains all the statements in which there is possibility of exception occurrence
catch: This block contains all the statements to handle the exception that is raised with in the try block
finally: This contains the statements to be executed compulsory, through try block is executed or catch block is executed
Execution:

  • Execution starts from try block, if there any exception occurs in any statement of try block then following lines of exception  statement are ignored and control jumps to catch block, catch block is executed and then finally block
  • If no exception occurs in any statement of try block then all statements of try block are executed and catch block is ignored then finally block is executed
  • We can also write Nested try, catches like…

try
{
          try
          {
                   Statements…
          }
          catch[(ExceptionClass Object)]
          {
                   Statements…
          }
          finally
          {
                   Statements…
          }
}
catch[Exception Class Object)]
{
          try
          {
                   Statements….
          }
          catch[ExceptionClass Object)]
          {
                   Statements….
          }
          finally
          {
                   Statements….
          }
}
finally
{
          Statements…..
}

No comments:

Post a Comment