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
If the derived class method does not throw the exception that the base class threw, it can either:
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
ExceptionClass Constructors and Methods
There are several forms of constructors defined in the base class for the exception hierarchy.
||Constructs a new throwable with
||Constructs a new throwable with the specified detail message.|
||Constructs a new throwable with the specified detail message and cause.|
|| Constructs a new throwable with the
specified cause and a detail message of
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.
throws Exception, they settled on
throws IOException, ServletException(or
JSPExceptionfor Java Server Pages).
ServletExceptionobjects if you did not want to handle them
Exception object has several useful methods:
||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;
||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).|
||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.