Sunday, 17 May 2015

Overview of Java exception handling

Exception Handling in Java provides a way to handle a situation when an exception is thrown and shows a meaningful message to the user and continue with the flow of the program.

How that is done

When an exceptional condition occurs with in a method, the method (where the exception occurred) creates an Exception Object and throws it. The created exception object contains information about the error, its type and the state of the program when the error occurred.
The method where the exception is thrown may handle that exception itself or pass it on. In case it passes it on, run time system goes through the method hierarchy that had been called to get to the current method to search for a method that can handle the exception.
If your program is not able to catch any particular exception, that will ultimately be processed by the default handler. The default handler displays a message describing the exception, prints a stack trace from the point at which the exception occurred, and terminates the program.

Five keywords used to manage Java exception handling

  • try - Any code that might throw an exception is enclosed within a try block.
  • catch - If an exception occurs in try block, catch block can provide exception handlers to handle it in a rational manner.
  • finally - The finally block always executes when the try block exits. So, any code that must execute after a try block is completed should be put in finally block.
  • throw - throw is used to manually thrown an exception.
  • throws - Any exception that is thrown in a method but not handled there must be specified in a throws clause.

General form of excpetion handling in Java

The general form of an exception-handling block -
try {
// block of code
}
catch (ExceptionType1 expObj) {
// exception handler for ExceptionType1
}
catch (ExceptionType2 expObj) {
// exception handler for ExceptionType2
}
// ...
finally {
// block of code to be executed after try block ends
}

Here, ExceptionType is the type of exception that has occurred

Exception Hierarchy

Throwable class is the super class of all the exception types. Below Throwable class there are two subclasses which denotes two distinct branches of exceptions -

  • Exception - An Exception indicates that a problem has occurred, but it is not a serious system problem. The user programs you write will throw and catch Exceptions.
  • Error - It defines exceptions that are not expected to be caught by your program. Exceptions of type Error are used by the Java run-time system to indicate errors having to do with the run-time environment, itself.
    Examples of error are StackOverflowError, OutOfMemoryError etc.
Exception Hierarchy

Kinds of exception

  • Checked Exception - These are exceptional conditions that an application should anticipate and recover from.
  • RunTime Excpetion - These are exceptional conditions that are external to the application, and the application usually cannot anticipate or recover from them.
  • Error - It defines exceptions that are not expected to be caught by your program.

  • Error and runtime exceptions are collectively known as unchecked exceptions.

Please refer to checked exception Vs unchecked exception for detailed explanation of checked and unchecked exceptions.

Advantages of Exception Handling

  • To maintain the normal flow of the application.
  • Separation of concerns by separating error-handling code from regular code.
  • Providing a proper message for the failing condition.
  • Preventing the program from automatically terminating.

Topics discussed in this exception handling tutorial

  1. try-catch block
  2. finally block
  3. Multiple catch blocks
  4. Nested try statements
  5. throw statement
  6. throws clause
  7. Exception Propagation
  8. Exception handling and method overriding
  9. Difference between Checked exception & Unchecked exception
  10. Difference between throw and throws
  11. final Vs finally Vs finalize
  12. Creating custom exception class
  13. Multi catch statement in Java 7
  14. try-with-resources in Java 7
  15. Best practices for exception handling in Java

>>>Go to Java Basics page

2 comments:

  1. You are doing great work, I would appreciate your efforts in helping us understand the concepts. I had a small doubt, throws clause is for the exceptions which are expected to be thrown by a method but not handled in the method is it? your explanation is little different from that. Can you help me understand which is correct.
    Thanks a lot!

    ReplyDelete
    Replies
    1. I said almost the same thing. A method will be called from some other method (may be main method). If you want to leave the task of exception handling to the calling method, in the called method you can just give the exceptions with throws clause.

      throws clause

      Delete