Implications for Exception Handling

When an exception is thrown in a try block, it is matched against the parameter of each catch block that is associated with the try block. This test is similar to the instance test, requiring reifiable types. The following restrictions apply:

• The type of the parameter of a catch block must be a reifiable type and it must also be a subtype of Throwable.

• A generic type cannot extend the Throwable class.

• A parameterized type cannot be specified in a throws clause.

The following example illustrates the three restrictions:

// (1) A generic class can not extend Exception:class MyGenericException<T> extends Exception { }         // Compile-time error!public class ExceptionErrors {  // (2) Cannot specify parameterized types in throws clause: ...

Get A Programmer’s Guide to Java™ SCJP Certification: A Comprehensive Primer, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.