View Javadoc
1 /*** 2 * Copyright (c) 2002, Reuters America Inc. All rights reserved.<p> 3 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following 4 * conditions are met:<p> 5 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 6 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer 7 * in the documentation and/or other materials provided with the distribution. Neither the name of Reuters America Inc. nor the 8 * names of its contributors may be used to endorse or promote products derived from this software without specific prior written 9 * permission.<p> 10 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 11 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 12 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 13 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 14 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 15 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.<p> 16 */ 17 18 package com.reuters.rc.db; 19 20 import java.io.PrintStream; 21 import java.io.PrintWriter; 22 import java.io.Writer; 23 import java.io.StringWriter; 24 25 /*** 26 * The DbBaseException class is used across all core services. 27 * 28 * It allows you to set a message with an exception, much like the 29 * normal java.lang.Exception class but in addition you can also carry 30 * along an original exception if you want. This 31 * is incorportated in the output from getMessage and toString(). 32 * 33 * The previous version of this class used to check if the string 34 * passed in was part of the MessageCatalog and if so, then use 35 * the associated key in creating the exception message. Various constructors 36 * were also provided to do string replacement on the exception message. 37 * This functionality is now removed. It is the responsibility of the 38 * creator of the exception to lookup message text directly from the 39 * MessageCatalog and pass it to the constructor of the exception. 40 * 41 * Implementation inspired from various placed, most recently 42 * Javaworld, Javatips #91. Java 1.4 now has this functionality. 43 * 44 * @author Mark Pollack 45 * @version @VSS_FLAG_VERSION@ 46 */ 47 public class DbBaseException extends Exception { 48 49 /*** 50 * Do nothing constructor. 51 * 52 */ 53 public DbBaseException() {} 54 55 /*** 56 * Creates a new <code>DbBaseException</code> instance with a given 57 * message string. 58 * 59 * @param message a <code>String</code> value 60 */ 61 public DbBaseException(String message) { 62 super(message); 63 } 64 65 /*** 66 * Create an exception nesting the rootCause of this exception. 67 * 68 * @param rootCause a value of type 'Throwable'. 69 */ 70 public DbBaseException(Throwable rootCause) { 71 super(); 72 if (rootCause != null) { 73 this.previousThrowable = rootCause; 74 stackTraceString = generateStackTraceString(rootCause); 75 } else { 76 this.previousThrowable = new Exception("ERROR: A null value for rootCause was passed to the constructor of DbBaseException"); 77 stackTraceString = generateStackTraceString(this.previousThrowable); 78 } 79 } 80 81 82 /*** 83 * Create a new <code>DbBaseException</code> instance. Allows for 84 * an association of an originating exception. 85 * 86 * @param message Message to describe the exception 87 * @param rootCause The exceptio that caused this exception to be raised. 88 */ 89 public DbBaseException(String message, Throwable rootCause) { 90 super(message); 91 if (rootCause != null) { 92 this.previousThrowable = rootCause; 93 stackTraceString = generateStackTraceString(rootCause); 94 } else { 95 this.previousThrowable = new Exception("ERROR: A null value for rootCause was passed to the constructor of DbBaseException"); 96 stackTraceString = generateStackTraceString(this.previousThrowable); 97 } 98 } 99 100 /*** 101 * Create a new <code>DbBaseException</code> instance. Allows for 102 * an association of an originating exception. 103 * 104 * @param message Message to describe the exception 105 * @param inserts an <code>Object[]</code> value 106 * @param rootCause The exceptio that caused this exception to be raised. 107 */ 108 public DbBaseException(String message, Object[] inserts, Throwable rootCause) { 109 super(message); 110 if (rootCause != null) { 111 this.previousThrowable = rootCause; 112 stackTraceString = generateStackTraceString(rootCause); 113 } else { 114 this.previousThrowable = new Exception("ERROR: A null value for rootCause was passed to the constructor of DbBaseException"); 115 stackTraceString = generateStackTraceString(this.previousThrowable); 116 } 117 } 118 119 /*** 120 * Create a new <code>DbBaseException</code> instance. Allows for 121 * an association of an originating exception. 122 * 123 * @param message Message to describe the exception 124 * @param insert an <code>Object</code> value 125 * @param rootCause The exceptio that caused this exception to be raised. 126 */ 127 public DbBaseException(String message, Object insert, Throwable rootCause) { 128 super(message); 129 if (rootCause != null) { 130 this.previousThrowable = rootCause; 131 stackTraceString = generateStackTraceString(rootCause); 132 } else { 133 this.previousThrowable = new Exception("ERROR: A null value for rootCause was passed to the constructor of DbBaseException"); 134 stackTraceString = generateStackTraceString(this.previousThrowable); 135 } 136 } 137 138 /*** 139 * Get the exception that caused this exception to be raised. 140 * 141 * @return the root exception. 142 */ 143 public Throwable getRootCause() { 144 return previousThrowable; 145 } 146 147 148 149 /*** 150 * Return the stack trace including root cause exceptions. 151 * 152 * @return The stack trace of the exception. 153 */ 154 public String getStackTraceString() { 155 // if there's no nested exception, there's no stackTrace 156 if (previousThrowable == null) 157 return null; 158 159 StringBuffer traceBuffer = new StringBuffer(); 160 161 if (previousThrowable instanceof DbBaseException) { 162 163 traceBuffer.append(((DbBaseException)previousThrowable).getStackTraceString()); 164 traceBuffer.append("-------- nested by:\n"); 165 } 166 167 traceBuffer.append(stackTraceString); 168 return traceBuffer.toString(); 169 } 170 171 // overrides Exception.getMessage() 172 /*** 173 * Override Exceptin.getMessage to include information of the root 174 * causeo of the exception. 175 * 176 * @return The exception message, including root cause messages. 177 */ 178 public String getMessage() { 179 // superMsg will contain whatever String was passed into the 180 // constructor, and null otherwise. 181 String superMsg = super.getMessage(); 182 183 // if there's no nested exception, do like we would always do 184 if (getRootCause() == null) 185 return superMsg; 186 187 StringBuffer theMsg = new StringBuffer(); 188 189 // get the nested exception's message 190 String nestedMsg = getRootCause().getMessage(); 191 192 if (superMsg != null) 193 theMsg.append(superMsg).append(": ").append(nestedMsg); 194 else 195 theMsg.append(nestedMsg); 196 197 return theMsg.toString(); 198 } 199 200 // overrides Exception.toString() 201 public String toString() { 202 StringBuffer theMsg = new StringBuffer(super.toString()); 203 204 if (getRootCause() != null) 205 theMsg.append("; \n\t---> nested ").append(getRootCause()); 206 207 return theMsg.toString(); 208 } 209 210 211 212 //TODO: hmmmm what about these..... 213 214 public void printStackTrace() 215 { 216 super.printStackTrace(); 217 if (this.previousThrowable != null) 218 { 219 this.previousThrowable.printStackTrace(); 220 } 221 } 222 223 public void printStackTrace(PrintStream inPrintStream) 224 { 225 super.printStackTrace(inPrintStream); 226 if (this.previousThrowable != null) 227 { 228 this.previousThrowable.printStackTrace(inPrintStream); 229 } 230 } 231 232 233 public void printStackTrace(PrintWriter inPrintWriter) 234 { 235 super.printStackTrace(inPrintWriter); 236 if (this.previousThrowable != null) 237 { 238 this.previousThrowable.printStackTrace(inPrintWriter); 239 } 240 } 241 242 243 static public String generateStackTraceString(Throwable t) { 244 StringWriter s = new StringWriter(); 245 t.printStackTrace(new PrintWriter(s)); 246 return s.toString(); 247 248 } 249 250 /* 251 * By giving <code>DbBaseException</code> a reference to a Throwable object, 252 * exception chaining can be enforced easily. 253 */ 254 private Throwable previousThrowable = null; 255 private String stackTraceString; 256 } 257

This page was automatically generated by Maven