O'Reilly logo

Programmer's Guide to Java™ Certification, A: A Comprehensive Primer, Second Edition by Rolf W. Rasmussen, Khalid A. Mughal

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

5.8. throw Statement

Earlier examples in this chapter have shown how an exception is thrown implicitly during execution. A program can explicitly throw an exception using the throw statement. The general format of the throw statement is as follows:

throw <object reference expression>;

The compiler ensures that the <object reference expression> is of type Throwable class or one of its subclasses. At runtime a NullPointerException is thrown if the <object reference expression> is null. This ensure that a Throwable will always be propagated. A detail message is often passed to the constructor when the exception object is created.

throw new ArithmeticException("Integer division by 0");

When an exception is thrown, normal execution is suspended. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required