Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

How the Java virtual machine handles exceptions

A detailed study with examples of classes and methods

  • Print
  • Feedback
Welcome to another installment of Under The Hood. This column aims to give Java developers a glimpse of the mysterious mechanisms clicking and whirring beneath their running Java programs. This month's article continues the discussion of the bytecode instruction set of the Java virtual machine by examining the manner in which the Java virtual machine handles exception throwing and catching, including the relevant bytecodes. This article does not discuss finally clauses -- that's next month's topic. Subsequent articles will discuss other members of the bytecode family.

Exceptions

Exceptions allow you to smoothly handle unexpected conditions that occur as your programs run. To demonstrate the way the Java virtual machine handles exceptions, consider a class named NitPickyMath that provides methods that perform addition, subtraction, multiplication, division, and remainder on integers. NitPickyMath performs these mathematical operations the same as the normal operations offered by Java's "+", "-", "*", "/", and "%" operators, except the methods in NitPickyMath throw checked exceptions on overflow, underflow, and divide-by-zero conditions. The Java virtual machine will throw an ArithmeticException on an integer divide-by-zero, but will not throw any exceptions on overflow and underflow. The exceptions thrown by the methods of NitPickyMath are defined as follows:

class OverflowException extends Exception {
}
class UnderflowException extends Exception {
}
class DivideByZeroException extends Exception {
}


A simple method that catches and throws exceptions is the remainder method of class NitPickyMath:

static int remainder(int dividend, int divisor)
    throws DivideByZeroException {
    try {
        return dividend % divisor;
    }
    catch (ArithmeticException e) {
        throw new DivideByZeroException();
    }
}


The remainder method simply performs the remainder operation upon the two ints passed as arguments. The remainder operation throws an ArithmeticException if the divisor of the remainder operation is a zero. This method catches this ArithmeticException and throws a DivideByZeroException.

The difference between a DivideByZero and an ArithmeticException exception is that the DivideByZeroException is a checked exception and the ArithmeticException is unchecked. Because the ArithmeticException is unchecked, a method need not declare this exception in a throws clause even though it might throw it. Any exceptions that are subclasses of either Error or RuntimeException are unchecked. (ArithmeticException is a subclass of RuntimeException.) By catching ArithmeticException and then throwing DivideByZeroException, the remainder method forces its clients to deal with the possibility of a divide-by-zero exception, either by catching it or declaring DivideByZeroException in their own throws clauses. This is because checked exceptions, such as DivideByZeroException, thrown within a method must be either caught by the method or declared in the method's throws clause. Unchecked exceptions, such as ArithmeticException, need not be caught or declared in the throws clause.

  • Print
  • Feedback

Resources
  • The book The Java Virtual Machine Specification, by Tim Lindholm and Frank Yellin (ISBN 0-201-63452-X), part of The Java Series (http://www.aw.com/cp/javaseries.html), from Addison-Wesley, is be the definitive Java virtual machine reference.
    http://www.aw.com/cp/lindholm-yellin.html
  • Previous Under The Hood articles
  • The lean, mean virtual machine -- Gives an introduction to the Java virtual machine. Look here to see how the garbage collected heap fits in with the other parts of the Java virtual machine.
  • The Java class file lifestyle -- Gives an overview to the Java class file, the file format into which all Java programs are compiled.
  • Java's garbage-collected heap -- Gives an overview of garbage collection in general and the garbage-collected heap of the Java virtual machine in particular.
  • Bytecode basics -- Introduces the bytecodes of the Java virtual machine, and discusses primitive types, conversion operations, and stack operations in particular.
  • Floating Point Arithmetic -- Describes the Java virtual machine's floating point support and the bytecodes that perform floating point operations.
  • Logic and Arithmetic -- Describes the Java virtual machine's support for logical and integer arithmetic, and the related bytecodes.
  • Objects and Arrays -- Describes how the Java virtual machine deals with objects and arrays, and discusses the relevant bytecodes.