1 16 package org.apache.commons.math; 17 18 import java.io.PrintStream ; 19 import java.io.PrintWriter ; 20 21 22 31 public class MathException extends Exception { 32 33 34 static final long serialVersionUID = -8594613561393443827L; 35 36 39 private static final boolean JDK_SUPPORTS_NESTED; 40 41 static { 42 boolean flag = false; 43 try { 44 Throwable .class.getDeclaredMethod("getCause", new Class [0]); 45 flag = true; 46 } catch (NoSuchMethodException ex) { 47 flag = false; 48 } 49 JDK_SUPPORTS_NESTED = flag; 50 } 51 52 55 private final Throwable rootCause; 56 57 61 public MathException() { 62 super(); 63 this.rootCause = null; 64 } 65 66 72 public MathException(String msg) { 73 super(msg); 74 this.rootCause = null; 75 } 76 77 84 public MathException(Throwable rootCause) { 85 super((rootCause == null ? null : rootCause.getMessage())); 86 this.rootCause = rootCause; 87 } 88 89 97 public MathException(String msg, Throwable rootCause) { 98 super(msg); 99 this.rootCause = rootCause; 100 } 101 102 107 public Throwable getCause() { 108 return rootCause; 109 } 110 111 114 public void printStackTrace() { 115 printStackTrace(System.err); 116 } 117 118 123 public void printStackTrace(PrintStream out) { 124 synchronized (out) { 125 PrintWriter pw = new PrintWriter (out, false); 126 printStackTrace(pw); 127 pw.flush(); 129 } 130 } 131 132 137 public void printStackTrace(PrintWriter out) { 138 synchronized (out) { 139 super.printStackTrace(out); 140 if (rootCause != null && JDK_SUPPORTS_NESTED == false) { 141 out.print("Caused by: "); 142 rootCause.printStackTrace(out); 143 } 144 } 145 } 146 147 } 148 | Popular Tags |