import java.io.PrintStream; import java.io.PrintWriter; /** * Thrown to indicate a fatal problem. **/ public class Problem extends RuntimeException { private Throwable cause_ = null; /** * Create an instance initialized with 'message' that does not have a * cause. **/ public Problem(String message) { super(message); } /** * Create and instance initialized with 'cause' and 'message'. **/ public Problem(Throwable cause, String message) { super(message); cause_ = cause; } /** * Return the cause of this problem, or null. **/ public Throwable getCause() { return cause_; } /** * Return the message for this problem. If this has a cause, then the * return value includes the problem message, newline, cause message. **/ public String getMessage() { if (cause_ == null) { return super.getMessage(); } else { StringBuffer message = new StringBuffer(); String superMessage = super.getMessage(); if (superMessage != null) { message.append(superMessage); message.append(System.getProperty("line.separator")); } message.append(cause_.getMessage()); return message.toString(); } } /** * Equivalent to {@link #printStackTrace(java.io.PrintStream) * printStackTrace(System.err)}. **/ public void printStackTrace() { printStackTrace(System.err); } /** * Print the stack trace to 's'. If this has a cause, then print its * message, "Caused by", and the cause stack trace. **/ public void printStackTrace(PrintStream s) { if (cause_ == null) { super.printStackTrace(s); } else { synchronized (s) { s.println(getClass().getName()+": "+super.getMessage()); s.print("Caused by "); cause_.printStackTrace(s); } } } /** * Print the stack trace to 's'. If this has a cause, then print its * message, "Caused by", and the cause stack trace. **/ public void printStackTrace(PrintWriter s) { if (cause_ == null) { super.printStackTrace(s); } else { synchronized (s) { s.println(getClass().getName()+": "+super.getMessage()); s.print("Caused by "); cause_.printStackTrace(s); } } } }