You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NestedRuntimeException.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright 2006 The Apache Software Foundation.
  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. /* $Id$ */
  17. package org.apache.fop.render.afp.exceptions;
  18. import java.io.PrintStream;
  19. import java.io.PrintWriter;
  20. /**
  21. * Handy class for wrapping runtime Exceptions with a root cause.
  22. * This technique is no longer necessary in Java 1.4, which provides
  23. * built-in support for exception nesting. Thus exceptions in applications
  24. * written to use Java 1.4 need not extend this class.
  25. *
  26. */
  27. public abstract class NestedRuntimeException extends RuntimeException {
  28. /** Root cause of this nested exception */
  29. private Throwable _underlyingException;
  30. /**
  31. * Construct a <code>NestedRuntimeException</code> with the specified detail message.
  32. * @param msg The detail message.
  33. */
  34. public NestedRuntimeException(String msg) {
  35. super(msg);
  36. }
  37. /**
  38. * Construct a <code>NestedRuntimeException</code> with the specified
  39. * detail message and nested exception.
  40. * @param msg The detail message.
  41. * @param t The nested exception.
  42. */
  43. public NestedRuntimeException(String msg, Throwable t) {
  44. super(msg);
  45. _underlyingException = t;
  46. }
  47. /**
  48. * Gets the original triggering exception
  49. * @return The original exception as a throwable.
  50. */
  51. public Throwable getUnderlyingException() {
  52. return _underlyingException;
  53. }
  54. /**
  55. * Return the detail message, including the message from the nested
  56. * exception if there is one.
  57. * @return The detail message.
  58. */
  59. public String getMessage() {
  60. if (_underlyingException == null) {
  61. return super.getMessage();
  62. } else {
  63. return super.getMessage()
  64. + "; nested exception is "
  65. + _underlyingException.getClass().getName();
  66. }
  67. }
  68. /**
  69. * Print the composite message and the embedded stack trace to the specified stream.
  70. * @param ps the print stream
  71. */
  72. public void printStackTrace(PrintStream ps) {
  73. if (_underlyingException == null) {
  74. super.printStackTrace(ps);
  75. } else {
  76. ps.println(this);
  77. _underlyingException.printStackTrace(ps);
  78. }
  79. }
  80. /**
  81. * Print the composite message and the embedded stack trace to the specified writer.
  82. * @param pw the print writer
  83. */
  84. public void printStackTrace(PrintWriter pw) {
  85. if (_underlyingException == null) {
  86. super.printStackTrace(pw);
  87. } else {
  88. pw.println(this);
  89. _underlyingException.printStackTrace(pw);
  90. }
  91. }
  92. }