Try – Catch block in Java

This “Try – Catch block in Java Exceptions” page is providing information about use of try-catch block including syntax and how to handle exception using try-catch block.

core-Java-online-Training-4

Try-Catch :

try-block:

  • It is a pre-defined keyword.
  • try-block contains doubt ful code that raised exception.
  • try-block create and throw an exception object in case of runtime error.

 

catch-block:

  • It is a keyword.
  • catch-block contains Exception handling code.
  • Catch block collects exception object address to be handled.
  • Catch block executes only in case of exception in try block.

 

Syntax:

try
{
1. Doubtful code that raises exception
2. Related info to exception
}
catch(<Exception_type> <identifier>)
{
Exception Handling code….
}

<Exception_type> specifies class name of Exception.
<Identifier> specifies object reference variable that hold exception object reference.

 

  • Exception object can be collected into Parent type variable.
  • Accessing Child using Parent reference is called Runtime Polymorphism.

core-Java-online-Training-4

Share this