1 /* 2 * Copyright 2007 Ralf Joachim 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 * 14 * $Id: Configuration.java 6907 2007-03-28 21:24:52Z rjoachim $ 15 */ 16 package org.castor.core.exceptions; 17 18 import java.io.PrintStream; 19 import java.io.PrintWriter; 20 21 /** 22 * CastorRuntimeException is the superclass of all unchecked Castor exceptions that are thrown 23 * during the normal operation of the Java Virtual Machine. 24 * 25 * @version $Id: Configuration.java,v 1.8 2006/03/08 17:25:52 jens Exp $ 26 * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a> 27 * @since 1.1.3 28 */ 29 public class CastorRuntimeException extends RuntimeException { 30 31 /** SerialVersionUID */ 32 private static final long serialVersionUID = 3984585622253325513L; 33 34 /** The cause of this exception or <code>null</code> if the cause is nonexistent or unknown. */ 35 private Throwable _cause = null; 36 37 /** Has the cause of this exception been initialized? */ 38 private boolean _initCause = false; 39 40 /** 41 * Constructs a new Castor runtime exception without a message. The cause is not initialized but 42 * may subsequently be initialized by a call to initCause(Throwable). 43 */ 44 public CastorRuntimeException() { 45 super(); 46 } 47 48 /** 49 * Constructs a new Castor runtime exception with the specified detail message. The cause is not 50 * initialized but may subsequently be initialized by a call to initCause(Throwable). 51 * 52 * @param message The detail message. 53 */ 54 public CastorRuntimeException(final String message) { 55 super(message); 56 } 57 58 /** 59 * Constructs a new Castor runtime exception with the specified cause and the detail message of 60 * the cause. This constructor is useful for exceptions that are wrappers for others. 61 * 62 * @param cause The cause. 63 */ 64 public CastorRuntimeException(final Throwable cause) { 65 super((cause == null) ? null : cause.getMessage()); 66 _cause = cause; 67 _initCause = true; 68 } 69 70 /** 71 * Constructs a new Castor runtime exception with the specified detail message and cause. 72 * 73 * @param message The detail message. 74 * @param cause The cause. 75 */ 76 public CastorRuntimeException(final String message, final Throwable cause) { 77 super(message); 78 _cause = cause; 79 _initCause = true; 80 } 81 82 /** 83 * The method emulates the JDK 1.4 Throwable version of initCause() for JDKs before 1.4. <br/> 84 * {@inheritDoc} 85 */ 86 public final Throwable initCause(final Throwable cause) { 87 if (cause == this) { 88 throw new IllegalArgumentException(); 89 } 90 if (_initCause) { 91 throw new IllegalStateException(); 92 } 93 _cause = cause; 94 _initCause = true; 95 return this; 96 } 97 98 /** 99 * The method emulates the JDK 1.4 Throwable version of getCause() for JDKs before 1.4. <br/> 100 * {@inheritDoc} 101 */ 102 public final Throwable getCause() { 103 return _cause; 104 } 105 106 /** 107 * {@inheritDoc} 108 */ 109 public void printStackTrace() { 110 // Print the stack trace for this exception. 111 super.printStackTrace(); 112 113 if (_cause != null) { 114 System.err.print("Caused by: "); 115 _cause.printStackTrace(); 116 } 117 } 118 119 /** 120 * {@inheritDoc} 121 */ 122 public final void printStackTrace(final PrintStream s) { 123 // Print the stack trace for this exception. 124 super.printStackTrace(s); 125 126 if (_cause != null) { 127 s.print("Caused by: "); 128 _cause.printStackTrace(s); 129 } 130 } 131 132 /** 133 * {@inheritDoc} 134 */ 135 public final void printStackTrace(final PrintWriter w) { 136 // Print the stack trace for this exception. 137 super.printStackTrace(w); 138 139 if (_cause != null) { 140 w.print("Caused by: "); 141 _cause.printStackTrace(w); 142 } 143 } 144 }