Java Exception Handling Tutorial-2

1:38 AM 48 Comments A+ a-

Advanced Exception Handling in Java Tutorial with Example Programs

This post is in continuation of the previous Java Exception Handling Tutorial. After reading the previous post, the basic concept of using try and catch block should be clear. The other thing that is to be noted in context of using try and catch block is that try block can not exist alone, it must be followed by at least one catch block or finally block. The next step that you should learn about is the nesting of try blocks.

Nesting Try Blocks

The try statement can be nested. That is, a try statement can be inside the block of another try. Each time a try statement is entered, the context of that exception is pushed on the stack. If an inner try statement does not have a catch handler for a particular exception, the stack is unwound and the next try statement's catch handlers are inspected for a match. This continues until one of the catch statements succeeds, or until all of the nested try statements are exhausted. If no catch statement matches, then the Java run-time system will handle the exception.
Below example program demonstates how the nesting of try blocks work in Java exception handling.

Program to demonstate nesting of try blocks in Java Exception Handling

Java Exception Handling Tutorial

1:00 AM 13 Comments A+ a-

Exception Handling in Java Tutorial with Example Programs

Exceptions in Java - What are Java Exceptions?

A Java Exception is an abnormal condition that arises during the execution of a program and also called a run-time error. An exception in Java signals the unusual or catastrophic situations that can arise.
Examples of Java Exceptions are:
  • Incorrect user input such as division by zero
  • File that needs to be opened not found
  • Network connection problem

Types of Java Exceptions

Like anything else in this world, Exceptions can also be categorized! Based on whether or not you are required to handle the exception, there are two types of exceptions: Checked and Unchecked Exceptions.
Java Exception Class Heirarcy
Exception Class Heirarchy
Checked Exceptions: These are the type of exceptions for which the compiler checks to ensure that your code is prepared for handling such exceptions. These are the exceptions that you can expect to occur frequently and you must handle them in your code. The conditions that generate such exceptions are generally outside the control of your program and they can occur in a correct program. But you can anticipate them and thus you must write code to deal with them. Programmatically, checked exceptions are the instances of the Exception class or one of its subclasses, excluding RuntimeException subtree.

Unchecked Exceptions: The compiler does not check for such type of exceptions. Unchecked Exceptions comprise of  run time exceptions (of type RuntimeException or its subclasses) and errors (of type Error or its subclasses). Runtime Exceptions occur due to program bugs and include exceptions such as division by zero and invalid array indexing. You can not recover from an unchecked exception and you are not required to handle such type of exceptions either, but still you can do so if you want. ArithmeticException class is an example of unchecked exception. Errors are the exceptions that are not expected to be caught under normal circumstances by your program. They are used by the Java run time environment to indicate errors related to run time environment itself. Stack Overflow is an example of error.