1. /*
  2. * Copyright 2002-2004 the original author or authors.
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.core;
  17. import java.io.PrintStream;
  18. import java.io.PrintWriter;
  19. /**
  20. * Handy class for wrapping checked Exceptions with a root cause.
  21. *
  22. * <p>This time-honoured technique is no longer necessary in Java 1.4, which
  23. * finally provides built-in support for exception nesting. Thus exceptions in
  24. * applications written to use Java 1.4 need not extend this class. To ease
  25. * migration, this class mirrors Java 1.4's nested exceptions as closely as possible.
  26. *
  27. * <p>Abstract to force the programmer to extend the class. getMessage will include
  28. * nested exception information; printStackTrace etc will delegate to the wrapped
  29. * exception, if any.
  30. *
  31. * <p>The similarity between this class and the NestedCheckedException class is
  32. * unavoidable, as Java forces these two classes to have different superclasses
  33. * (ah, the inflexibility of concrete inheritance!).
  34. *
  35. * <p>As discussed in
  36. * <a href="http://www.amazon.com/exec/obidos/tg/detail/-/0764543857/">Expert One-On-One J2EE Design and Development</a>,
  37. * runtime exceptions are often a better alternative to checked exceptions.
  38. * However, all exceptions should preserve their stack trace, if caused by a
  39. * lower-level exception.
  40. *
  41. * @author Rod Johnson
  42. * @author Juergen Hoeller
  43. * @version $Id: NestedCheckedException.java,v 1.8 2004/03/29 20:51:27 jhoeller Exp $
  44. * @see #getMessage
  45. * @see #printStackTrace
  46. */
  47. public abstract class NestedCheckedException extends Exception {
  48. /** Root cause of this nested exception */
  49. private Throwable cause;
  50. /**
  51. * Construct a <code>ExceptionWrapperException</code> with the specified detail message.
  52. * @param msg the detail message
  53. */
  54. public NestedCheckedException(String msg) {
  55. super(msg);
  56. }
  57. /**
  58. * Construct a <code>RemoteException</code> with the specified detail message
  59. * and nested exception.
  60. * @param msg the detail message
  61. * @param ex the nested exception
  62. */
  63. public NestedCheckedException(String msg, Throwable ex) {
  64. super(msg);
  65. this.cause = ex;
  66. }
  67. /**
  68. * Return the nested cause, or null if none.
  69. */
  70. public Throwable getCause() {
  71. return (cause == this ? null : cause);
  72. }
  73. /**
  74. * Return the detail message, including the message from the nested exception
  75. * if there is one.
  76. */
  77. public String getMessage() {
  78. // Even if you cannot set the cause of this exception other than through
  79. // the constructor, we check for the cause being "this" here, as the cause
  80. // could still be set to "this" via reflection: for example, by a remoting
  81. // deserializer like Hessian's.
  82. if (this.cause == null || this.cause == this) {
  83. return super.getMessage();
  84. }
  85. else {
  86. return super.getMessage() + "; nested exception is " + this.cause.getClass().getName() +
  87. ": " + this.cause.getMessage();
  88. }
  89. }
  90. /**
  91. * Print the composite message and the embedded stack trace to the specified stream.
  92. * @param ps the print stream
  93. */
  94. public void printStackTrace(PrintStream ps) {
  95. if (this.cause == null || this.cause == this) {
  96. super.printStackTrace(ps);
  97. }
  98. else {
  99. ps.println(this);
  100. this.cause.printStackTrace(ps);
  101. }
  102. }
  103. /**
  104. * Print the composite message and the embedded stack trace to the specified print writer.
  105. * @param pw the print writer
  106. */
  107. public void printStackTrace(PrintWriter pw) {
  108. if (this.cause == null || this.cause == this) {
  109. super.printStackTrace(pw);
  110. }
  111. else {
  112. pw.println(this);
  113. this.cause.printStackTrace(pw);
  114. }
  115. }
  116. }