bcds.tools
Class AnyException

java.lang.Object
  extended by java.lang.Throwable
      extended by java.lang.Exception
          extended by java.lang.RuntimeException
              extended by bcds.tools.AnyException
All Implemented Interfaces:
java.io.Serializable

public class AnyException
extends java.lang.RuntimeException

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:

AnyException can be used to try to limit to a minimum the use of checked exceptions in Java programas. Whether that is desirable is the subject of an ongoing debate. If you do use this class, bear in mind that you will have two sources for exception identification: the plain (regular) exception objects, and the ones that are "inside" AnyException objects. This may lead to (undesirable) exception handlers such as:
   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.

Author:
Juan Segovia S.
See Also:
Serialized Form

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
<E extends java.lang.Throwable>
AnyException
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

AnyException

public AnyException(java.lang.Throwable e)
Creates a new instance, with parameter e as the "cause".

Method Detail

make

public static <E extends java.lang.Throwable> AnyException 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. 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);
    ...
 

Throws:
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.

printStackTrace

public void printStackTrace()
Prints the stack trace on stderr.

Overrides:
printStackTrace in class java.lang.Throwable

printStackTrace

public void printStackTrace(java.io.PrintStream s)
Prints the stack trace on the PrintStream s.

Overrides:
printStackTrace in class java.lang.Throwable

printStackTrace

public void printStackTrace(java.io.PrintWriter s)
Prints the stack trace on the PrintWriter s.

Overrides:
printStackTrace in class java.lang.Throwable

getMessage

public java.lang.String getMessage()
Returns the message associated to the "cause" exception, or null if such cause is null.

Overrides:
getMessage in class java.lang.Throwable

toString

public 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").

Overrides:
toString in class java.lang.Throwable

getCause

public java.lang.Throwable getCause()
Returns the "cause" exception wrapped in this instance. The cause is the first nested exception that is not an instance of AnyException.

Overrides:
getCause in class java.lang.Throwable