1 16 17 package org.springframework.core; 18 19 import java.io.PrintStream ; 20 import java.io.PrintWriter ; 21 22 45 public abstract class NestedCheckedException extends Exception { 46 47 48 private static final long serialVersionUID = 7100714597678207546L; 49 50 51 52 private Throwable cause; 53 54 55 59 public NestedCheckedException(String msg) { 60 super(msg); 61 } 62 63 69 public NestedCheckedException(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 133 public Throwable getRootCause() { 134 Throwable cause = getCause(); 135 if (cause instanceof NestedCheckedException) { 136 return ((NestedCheckedException) cause).getRootCause(); 137 } 138 else { 139 return cause; 140 } 141 } 142 143 151 public Throwable getMostSpecificCause() { 152 Throwable rootCause = getRootCause(); 153 return (rootCause != null ? rootCause : this); 154 } 155 156 165 public boolean contains(Class exType) { 166 if (exType == null) { 167 return false; 168 } 169 if (exType.isInstance(this)) { 170 return true; 171 } 172 Throwable cause = getCause(); 173 if (cause instanceof NestedCheckedException) { 174 return ((NestedCheckedException) cause).contains(exType); 175 } 176 else { 177 return (cause != null && exType.isInstance(cause)); 178 } 179 } 180 181 } 182 | Popular Tags |