Java: Exceptions

Exceptions

This Java video tutorial gives a brief overview of exceptions and the terms related to them. This lesson is only intended to give you some familiarity with exceptions and make you comfortable with the limited usage that we will make of them in the near future. Later on, we will give a more extensive treatment of exceptions.

An exception is an exceptional or unusual condition that arises during the course of a program's execution. The condition is typically something that is undesirable. So, we could also call it an error. Some examples of what could constitute an exception are running out of memory or dividing by zero. In either case, there is not an obvious answer as to how to continue the program. Hence it is up to the programmer to determine what memory he might want to free or what value he would like to assign to the calcuation, for example.

When an exception occurs, we say that the code throws an exception. In order to handle an exception, we must first catch it. This is the terminology that we use with exceptions: they are thrown and then caught.

Dealing with Exceptions

  1. Rethrow
  2. Catch

Some functions that we call can throw a particular type of exception. In order to compile programs that use these functions, we need to have a method for dealing with these exceptions. We can either rethrow the exception or catch it and handle it.

Since we have already given several simple exception code examples in them in the video, we will jump into a more complex code example here. Within the try block, the code below calls the System.in.reset() function, which throws a java.io.IOException exception. However, when the program below is executed, the code never reaches that code because the earlier division by zero throws an ArithmeticException first, which causes the rest of the code before the catch statements to be skipped.

Program 1

package test;

public class Test {
    public static void main(String[] args) {
        try {
            int iNumer = 10;
            int iDenom = 0;
            System.out.println("The Quotient is " + (iNumer/iDenom));
            System.out.println("This may not get executed.");

            System.in.reset();
            System.out.println("This may not get executed either.");
        } catch (ArithmeticException e) {
            System.out.println("ArithmeticException caught!");
        } catch (java.io.IOException e) {
            System.out.println("java.io.IOException caught!");
        }
        System.out.println("This will be executed.");
    }
}

Output 1

Output of program 1

The output above shows that the code after the division by zero is skipped. Notice also that there are two catch blocks, one for each type of exception.

The second catch is executed if we set the denominator to something that is nonzero, like 1. If we do that, we will see the output below when we execute the code.

Output with denominator set to 1

Output with denominator 1

This example demonstrates a slightly more complex exception code example than the ones in the video. However, all of the examples in this lesson are only intended to make you more comfortable with what you are likely to see in the code. We will deal with exceptions more thoroughly later. For now, it is sufficient to recognize that the commands try, catch, and throw, as well all references to exceptions, are parts of the program that you can ignore for the most part, since they deal with exceptions. It would be nice to ignore exceptions entirely for a while longer, but Java forces us to deal with them.