Tuesday, December 13, 2011

Exception Handling

  • Exception Handling is used to handle run time errors.
  • There are three block in exception handling mechanism.
                            1.try
                            2.catch
                            3.finally

try block:
  • The code which may raise exceptions should be written inside the try block.
catch block:
  • If the error is raised in try block, the catch block will catch that error and handle that error.
  • If the try block will not raise any exceptions, catch block will not be executed.
finally block:
  • The try block is used to clean up the resources, what we have opened for operations in the try block. (The resources may be files or database connections, etc..)
  • The try block will always executed if the try block exist, except the System.exit(0) call.
  • The finally block will executed, even if the error is not raised in try block.
Important Methods of Throwable class are:
  • getMessage(); //gives the error description
  • printStackTrace(); // gives the error path
  • getCause(); // cause is nested error object ==> Throwable t=e.getCause(); t1.printStackTrace();
Note: Throwable class is the base class for Exception class.

No comments:

Post a Comment