1 package org.apache.torque; 2 3 21 22 import java.io.PrintStream ; 23 import java.io.PrintWriter ; 24 import java.io.StringWriter ; 25 import java.util.LinkedList ; 26 import java.util.StringTokenizer ; 27 28 45 public class TorqueRuntimeException 46 extends RuntimeException 47 { 48 51 private static final long serialVersionUID = -2997617341459640541L; 52 53 57 private Throwable nested = null; 58 59 63 public TorqueRuntimeException() 64 { 65 super(); 66 } 67 68 74 public TorqueRuntimeException(String msg) 75 { 76 super(msg); 77 } 78 79 86 public TorqueRuntimeException(Throwable nested) 87 { 88 super(); 89 this.nested = nested; 90 } 91 92 100 public TorqueRuntimeException(String msg, Throwable nested) 101 { 102 super(msg); 103 this.nested = nested; 104 } 105 106 109 public void printStackTrace() 110 { 111 synchronized (System.err) 112 { 113 printStackTrace(System.err); 114 } 115 } 116 117 122 public void printStackTrace(PrintStream out) 123 { 124 synchronized (out) 125 { 126 PrintWriter pw = new PrintWriter (out, false); 127 printStackTrace(pw); 128 pw.flush(); 130 } 131 } 132 133 138 public void printStackTrace(PrintWriter out) 139 { 140 synchronized (out) 141 { 142 printStackTrace(out, 0); 143 } 144 } 145 146 153 public void printStackTrace(PrintWriter out, int skip) 154 { 155 String [] st = captureStackTrace(); 156 if (nested != null) 157 { 158 if (nested instanceof TorqueRuntimeException) 159 { 160 ((TorqueRuntimeException) nested) 161 .printStackTrace(out, st.length - 2); 162 } 163 else if (nested instanceof TorqueException) 164 { 165 ((TorqueException) nested).printStackTrace(out); 166 } 167 else 168 { 169 String [] nst = captureStackTrace(nested); 170 for (int i = 0; i < nst.length - st.length + 2; i++) 171 { 172 out.println(nst[i]); 173 } 174 } 175 out.print("rethrown as "); 176 } 177 for (int i = 0; i < st.length - skip; i++) 178 { 179 out.println(st[i]); 180 } 181 } 182 183 188 private String [] captureStackTrace() 189 { 190 StringWriter sw = new StringWriter (); 191 super.printStackTrace(new PrintWriter (sw, true)); 192 return splitStackTrace(sw.getBuffer().toString()); 193 } 194 195 202 private String [] captureStackTrace(Throwable t) 203 { 204 StringWriter sw = new StringWriter (); 205 t.printStackTrace(new PrintWriter (sw, true)); 206 return splitStackTrace(sw.getBuffer().toString()); 207 } 208 209 216 private String [] splitStackTrace(String stackTrace) 217 { 218 String linebreak = System.getProperty("line.separator"); 219 StringTokenizer st = new StringTokenizer (stackTrace, linebreak); 220 LinkedList list = new LinkedList (); 221 while (st.hasMoreTokens()) 222 { 223 list.add(st.nextToken()); 224 } 225 return (String []) list.toArray(); 226 } 227 } 228 | Popular Tags |