1 16 17 package org.springframework.core; 18 19 import java.io.PrintStream ; 20 import java.io.PrintWriter ; 21 22 45 public abstract class NestedRuntimeException extends RuntimeException { 46 47 48 private static final long serialVersionUID = 5439915454935047936L; 49 50 51 52 private Throwable cause; 53 54 55 59 public NestedRuntimeException(String msg) { 60 super(msg); 61 } 62 63 69 public NestedRuntimeException(String msg, Throwable cause) { 70 super(msg); 71 this.cause = cause; 72 } 73 74 75 80 public Throwable getCause() { 81 return (this.cause == this ? null : this.cause); 86 } 87 88 92 public String getMessage() { 93 return NestedExceptionUtils.buildMessage(super.getMessage(), getCause()); 94 } 95 96 100 public void printStackTrace(PrintStream ps) { 101 if (getCause() == null) { 102 super.printStackTrace(ps); 103 } 104 else { 105 ps.println(this); 106 ps.print("Caused by: "); 107 getCause().printStackTrace(ps); 108 } 109 } 110 111 115 public void printStackTrace(PrintWriter pw) { 116 if (getCause() == null) { 117 super.printStackTrace(pw); 118 } 119 else { 120 pw.println(this); 121 pw.print("Caused by: "); 122 getCause().printStackTrace(pw); 123 } 124 } 125 126 127 134 public Throwable getRootCause() { 135 Throwable cause = getCause(); 136 if (cause instanceof NestedRuntimeException) { 137 Throwable rootCause = ((NestedRuntimeException) cause).getRootCause(); 138 return (rootCause != null ? rootCause : cause); 139 } 140 else { 141 return cause; 142 } 143 } 144 145 153 public Throwable getMostSpecificCause() { 154 Throwable rootCause = getRootCause(); 155 return (rootCause != null ? rootCause : this); 156 } 157 158 167 public boolean contains(Class exType) { 168 if (exType == null) { 169 return false; 170 } 171 if (exType.isInstance(this)) { 172 return true; 173 } 174 Throwable cause = getCause(); 175 if (cause instanceof NestedRuntimeException) { 176 return ((NestedRuntimeException) cause).contains(exType); 177 } 178 else { 179 return (cause != null && exType.isInstance(cause)); 180 } 181 } 182 183 } 184 | Popular Tags |