WrappedException.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright (c) 1999, 2003, 2005 Per M.A. Bothner.
  2. // This is free software; for terms and warranty disclaimer see ./COPYING.
  3. package gnu.mapping;
  4. /** Encapsulate some Exception inside a RuntimeException.
  5. * Inspired by org.xml.sax.SAXException written by David Megginson.
  6. */
  7. public class WrappedException extends RuntimeException
  8. {
  9. /**
  10. * Create a new WrappedException.
  11. */
  12. public WrappedException ()
  13. {
  14. }
  15. /**
  16. * Create a new WrappedException.
  17. *
  18. * @param message The error or warning message.
  19. */
  20. public WrappedException (String message)
  21. {
  22. super(message);
  23. }
  24. /**
  25. * Create a new WrappedException wrapping an existing exception.
  26. *
  27. * <p>The existing exception will be embedded in the new
  28. * one, and its message will become the default message for
  29. * the WrappedException.</p>
  30. *
  31. * @param e The exception to be wrapped in a WrappedException.
  32. */
  33. public WrappedException (Throwable e)
  34. {
  35. this(e == null ? null : e.toString(), e);
  36. }
  37. /**
  38. * Create a new WrappedException from an existing exception.
  39. *
  40. * <p>The existing exception will be embedded in the new
  41. * one, but the new exception will have its own message.</p>
  42. *
  43. * @param message The detail message.
  44. * @param e The exception to be wrapped in a WrappedException.
  45. */
  46. public WrappedException (String message, Throwable e)
  47. {
  48. /* #ifdef use:java.lang.Throwable.getCause */
  49. super(message, e);
  50. /* #else */
  51. // super(message);
  52. // initCause(e);
  53. /* #endif */
  54. }
  55. /**
  56. * Return the embedded exception, if any.
  57. *
  58. * @return The embedded exception, or null if there is none.
  59. */
  60. public Throwable getException ()
  61. {
  62. return getCause();
  63. }
  64. /**
  65. * Convert this exception to a string.
  66. *
  67. * @return A string version of this exception.
  68. */
  69. public String toString ()
  70. {
  71. return getMessage();
  72. }
  73. // The initCause/getCause functionality was added in JDK 1.4.
  74. /* #ifndef use:java.lang.Throwable.getCause */
  75. // public Throwable initCause(Throwable cause)
  76. // {
  77. // exception = cause;
  78. // return this;
  79. // }
  80. // public Throwable getCause()
  81. // {
  82. // return exception;
  83. // }
  84. // private Throwable exception;
  85. /* #endif */
  86. /** Coerce argument to a RuntimeException.
  87. * Using rethrow may be preferable as it doesn't require wrapping an Error.
  88. */
  89. public static RuntimeException wrapIfNeeded (Exception ex)
  90. {
  91. if (ex instanceof RuntimeException)
  92. return (RuntimeException) ex;
  93. else
  94. return new WrappedException(ex);
  95. }
  96. /** Re-throw as a non-checked exception.
  97. * This method never returns, in spite of the return type.
  98. * This allows the call to be written as:
  99. * {@code throw WrappedExcepton.rethrow(ex)}
  100. * so javac and the verifier can know the code doesn't return.
  101. */
  102. public static RuntimeException rethrow (Throwable ex) {
  103. if (ex instanceof Error)
  104. throw (Error) ex;
  105. else if (ex instanceof RuntimeException)
  106. throw (RuntimeException) ex;
  107. else
  108. throw new WrappedException(ex);
  109. }
  110. }