Exceptions and Inheritance

Contact Us or call 1-877-932-8228
Exceptions and Inheritance

Exceptions and Inheritance

If a base class method throws an exception, that behavior will also occur in any derived classes that do not override the method.

An overriding method may throw the same exception(s) that the base class method threw.

An overriding method cannot add new exceptions to the throws list. Similar to placing more strict access on the method, this would restrict the derived class object in ways that a base class reference would be unaware of.

If the derived class method does not throw the exception that the base class threw, it can either:

  1. Retain the exception in the throws list, even though it does not throw it; this would enable subclasses to throw the exception.
  2. Remove the exception from its throws list, thus blocking subsequent extensions from throwing that exception.

If you have a base class method that does not throw an exception, but you expect that subclasses might, you can declare the base class to throw that exception.

Exception Class Constructors and Methods

There are several forms of constructors defined in the base class for the exception hierarchy.

Constructor Description
Throwable() Constructs a new throwable with null as its detail message.
Throwable(String message) Constructs a new throwable with the specified detail message.
Throwable(String message, Throwable cause) Constructs a new throwable with the specified detail message and cause.
Throwable(Throwable cause) Constructs a new throwable with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause).

The forms involving a cause are used in situations like Servlets and Java Server Pages, where a specific exception is thrown by the JSP engine, but it may be rooted in an exception from your code.

  • In both cases, a method in a base class is overridden by your code since the writers of the base class did not know what specific exceptions your code might throw, and didn't want to specify something too broad like throws Exception, they settled on throws IOException, ServletException (or JSPException for Java Server Pages).
  • You would try and catch for your expected exceptions and repackage them inside ServletException objects if you did not want to handle them

A Exception object has several useful methods:

Method Description
getMessage() Prints the message that was associated with the exception (many of the exceptions that deal with outside resources pass on the message from the outside) - for example, when you connect to a database and run a query, that could generate an error in the database; getMessage() will show that message.
printStackTrace() Prints to the standard error stream the trace of what function called what function, etc., leading up to the exception. There are variations of this method where you may specify a destination for the printing (note that stack trace includes the message).
printStackTrace(PrintStream stream) Same as above, but prints to the specified output stream (which could be hooked to a log file, for example).

Also worth noting is that the Java Logging API has logging methods that will accept a Throwable parameter and make a log entry with the stack trace.