View Javadoc
1 /*** 2 * Copyright 2003, 2004, 2005. CodeStreet LLC. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 7 * 8 * Unless required by applicable law or agreed to in writing, software 9 * distributed under the License is distributed on an "AS IS" BASIS, 10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 * See the License for the specific language governing permissions and 12 * limitations under the License. 13 */ 14 15 package com.codestreet.selector.parser; 16 17 import java.io.PrintStream; 18 import java.io.PrintWriter; 19 import java.io.StringWriter; 20 21 /*** 22 * Base class for exceptions. Allows exception chaining. 23 * 24 * @author Jawaid Hakim. 25 */ 26 public class BaseException extends Exception 27 { 28 /*** 29 * Ctor. 30 */ 31 public BaseException() 32 { 33 } 34 35 /*** 36 * Ctor. 37 * 38 * @param message 39 * a <tt>String</tt> value 40 */ 41 public BaseException(final String message) 42 { 43 super(message); 44 } 45 46 /*** 47 * Ctor. 48 * 49 * @param root 50 * Root cause of exception. Cannot be <tt>null</tt>. 51 */ 52 public BaseException(final Throwable root) 53 { 54 super(); 55 if (root != null) 56 { 57 this.previousCause = root; 58 stackTraceString = generateStackTraceString(root); 59 } 60 else 61 { 62 this.previousCause = new Exception( 63 "ERROR: A null value for root was passed to the constructor of RBaseException"); 64 stackTraceString = generateStackTraceString(this.previousCause); 65 } 66 } 67 68 /*** 69 * Ctor. 70 * 71 * @param message 72 * Message to describe the exception 73 * @param root 74 * Root cause of exception. Cannot be <tt>null</tt>. 75 */ 76 public BaseException(final String msg, final Throwable root) 77 { 78 super(msg); 79 if (root != null) 80 { 81 this.previousCause = root; 82 stackTraceString = generateStackTraceString(root); 83 } 84 else 85 { 86 this.previousCause = new Exception( 87 "ERROR: A null value for root was passed to the constructor of RBaseException"); 88 stackTraceString = generateStackTraceString(this.previousCause); 89 } 90 } 91 92 /*** 93 * Create a new <tt>RBaseException</tt> instance. Allows for an 94 * association of an originating exception. 95 * 96 * @param msg 97 * Message to describe the exception 98 * @param inserts 99 * an <tt>Object[]</tt> value 100 * @param root 101 * Root cause of exception. Cannot be <tt>null</tt>. 102 */ 103 public BaseException(final String msg, final Object[] inserts, 104 final Throwable root) 105 { 106 super(msg); 107 if (root != null) 108 { 109 this.previousCause = root; 110 stackTraceString = generateStackTraceString(root); 111 } 112 else 113 { 114 this.previousCause = new Exception( 115 "ERROR: A null value for root was passed to the constructor of RBaseException"); 116 stackTraceString = generateStackTraceString(this.previousCause); 117 } 118 } 119 120 /*** 121 * Create a new <tt>RBaseException</tt> instance. Allows for an 122 * association of an originating exception. 123 * 124 * @param msg 125 * Message to describe the exception 126 * @param insert 127 * an <tt>Object</tt> value 128 * @param root 129 * Root cause of exception. Cannot be <tt>null</tt>. 130 */ 131 public BaseException(final String msg, final Object insert, 132 final Throwable root) 133 { 134 super(msg); 135 if (root != null) 136 { 137 this.previousCause = root; 138 stackTraceString = generateStackTraceString(root); 139 } 140 else 141 { 142 this.previousCause = new Exception( 143 "ERROR: A null value for rootCause was passed to the constructor of RBaseException"); 144 stackTraceString = generateStackTraceString(this.previousCause); 145 } 146 } 147 148 /*** 149 * Get root cause of exception. 150 * 151 * @return Root exception. 152 */ 153 public Throwable getRootCause() 154 { 155 return previousCause; 156 } 157 158 /*** 159 * Return the stack trace including root cause exceptions. 160 * 161 * @return The stack trace of the exception. 162 */ 163 public String getStackTraceString() 164 { 165 // if there's no nested exception, there's no stackTrace 166 if (previousCause == null) 167 return null; 168 169 StringBuffer traceBuffer = new StringBuffer(); 170 171 if (previousCause instanceof BaseException) 172 { 173 174 traceBuffer.append(((BaseException) previousCause) 175 .getStackTraceString()); 176 traceBuffer.append("-------- nested by:\n"); 177 } 178 179 traceBuffer.append(stackTraceString); 180 return traceBuffer.toString(); 181 } 182 183 // overrides Exception.getMessage() 184 /*** 185 * Override Exceptin.getMessage to include information of the root causeo of 186 * the exception. 187 * 188 * @return The exception msg, including root cause messages. 189 */ 190 public String getMessage() 191 { 192 String baseMsg = super.getMessage(); 193 194 // if there's no nested exception, do like we would always do 195 if (getRootCause() == null) 196 return baseMsg; 197 198 StringBuffer theMsg = new StringBuffer(); 199 200 // get the nested exception's msg 201 String previousMsg = getRootCause().getMessage(); 202 203 if (baseMsg != null) 204 theMsg.append(baseMsg).append(": ").append(previousMsg); 205 else 206 theMsg.append(previousMsg); 207 208 return theMsg.toString(); 209 } 210 211 // overrides Exception.toString() 212 public String toString() 213 { 214 StringBuffer theMsg = new StringBuffer(super.toString()); 215 216 if (getRootCause() != null) 217 theMsg.append("; \n\t---> nested ").append(getRootCause()); 218 219 return theMsg.toString(); 220 } 221 222 public void printStackTrace() 223 { 224 super.printStackTrace(); 225 if (this.previousCause != null) 226 { 227 this.previousCause.printStackTrace(); 228 } 229 } 230 231 public void printStackTrace(final PrintStream inPrintStream) 232 { 233 super.printStackTrace(inPrintStream); 234 if (this.previousCause != null) 235 { 236 this.previousCause.printStackTrace(inPrintStream); 237 } 238 } 239 240 public static String generateStackTraceString(final Throwable t) 241 { 242 StringWriter s = new StringWriter(); 243 t.printStackTrace(new PrintWriter(s)); 244 return s.toString(); 245 246 } 247 248 private Throwable previousCause = null; 249 250 private String stackTraceString; 251 }

This page was automatically generated by Maven