|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
bcds.tools.AnyException
public class AnyException
This class wraps any Exception (checked or unchecked) into a RuntimeException. The starting point for this class was the code by Bruce Eckel here The differences are:
try { whatever_may_fail(); } catch (AnyException ex) { if ( ex.getCause() instanceof NullPointerException) { doSomething(); } else if ( ex.getCause() instanceof IOException ) { ... } catch (NullPointerException ex) { ... } catch (IOException ex) { ... }Note that there are duplicate "catch" entries above (one with instanceof and the other with catch proper). A better alternative is to use nesting, as follows:
try { try { whatever_may_fail(); } catch (AnyException ex) { // Rethrow the real exception, that is, unwraps the exception. ex.rethrow(); } } catch (NullPointerException ex) { ex.printStackTrace(System.err); } catch (Exception ex) { System.err.printf("\n%s\n", ex.getMessage()); }Such a handler should be put as close a possible to the "main" program.
Constructor Summary | |
---|---|
AnyException(java.lang.Throwable e)
Creates a new instance, with parameter e as the
"cause". |
Method Summary | ||
---|---|---|
java.lang.Throwable |
getCause()
Returns the "cause" exception wrapped in this instance. |
|
java.lang.String |
getMessage()
Returns the message associated to the "cause" exception, or null if such cause is null. |
|
static
|
make(java.lang.Class<E> class_ref,
java.lang.String format,
java.lang.Object... args)
Returns a new AnyException instance with a dynamically created cause of class E . |
|
void |
printStackTrace()
Prints the stack trace on stderr. |
|
void |
printStackTrace(java.io.PrintStream s)
Prints the stack trace on the PrintStream s . |
|
void |
printStackTrace(java.io.PrintWriter s)
Prints the stack trace on the PrintWriter s . |
|
java.lang.String |
toString()
Returns a string consisting of the fully qualified name of this class, a colon and whatever string is produced by the toString method of the wrapped exception (the "cause"). |
Methods inherited from class java.lang.Throwable |
---|
fillInStackTrace, getLocalizedMessage, getStackTrace, initCause, setStackTrace |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public AnyException(java.lang.Throwable e)
e
as the
"cause".
Method Detail |
---|
public static <E extends java.lang.Throwable> AnyException make(java.lang.Class<E> class_ref, java.lang.String format, java.lang.Object... args)
E
. This is a convenience method designed to
facilitate writing simple throws with formatted messages.
The cause is a newly created exception of type E
with
the message set to String.format(format, args).
The new exception's stack is conveniently manipulated so that
this method does not appear in the stack trace.
Example of use:
... if ( some_condition_fails ) throw AnyException.make(IOException.class, "Reading file %d failed at line %d. Reason: %s", filename, last_line, failure_reason); ...
AnyException
- with cause set to NoSuchMethodException
if the constructor E(String) does not exist.
IllegalFormatException
- (one of its derived classes) thrown by
java.util.Formatter if there is any
error during the formatting of the message.
AnyException
- if instantiation of the cause fails.
The cause is set to that exception.public void printStackTrace()
printStackTrace
in class java.lang.Throwable
public void printStackTrace(java.io.PrintStream s)
s
.
printStackTrace
in class java.lang.Throwable
public void printStackTrace(java.io.PrintWriter s)
s
.
printStackTrace
in class java.lang.Throwable
public java.lang.String getMessage()
getMessage
in class java.lang.Throwable
public java.lang.String toString()
toString
in class java.lang.Throwable
public java.lang.Throwable getCause()
getCause
in class java.lang.Throwable
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |