KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > mapping > WrappedException


1 // Copyright (c) 1999, 2003, 2005 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.mapping;
5
6 /** Encapsulate some Exception inside a RuntimeException.
7  * Inspired by org.xml.sax.SAXException written by David Megginson.
8  */

9
10 public class WrappedException extends RuntimeException JavaDoc
11 {
12   /**
13    * Create a new WrappedException.
14    */

15   public WrappedException ()
16   {
17   }
18
19   /**
20    * Create a new WrappedException.
21    *
22    * @param message The error or warning message.
23    */

24   public WrappedException (String JavaDoc message)
25   {
26     super(message);
27   }
28
29   /**
30    * Create a new WrappedException wrapping an existing exception.
31    *
32    * <p>The existing exception will be embedded in the new
33    * one, and its message will become the default message for
34    * the WrappedException.</p>
35    *
36    * @param e The exception to be wrapped in a WrappedException.
37    */

38   public WrappedException (Throwable JavaDoc e)
39   {
40     this(e.toString(), e);
41   }
42
43   /**
44    * Create a new WrappedException from an existing exception.
45    *
46    * <p>The existing exception will be embedded in the new
47    * one, but the new exception will have its own message.</p>
48    *
49    * @param message The detail message.
50    * @param e The exception to be wrapped in a WrappedException.
51    */

52   public WrappedException (String JavaDoc message, Throwable JavaDoc e)
53   {
54     /* #ifdef use:java.lang.Throwable.getCause */
55     super(message, e);
56     /* #else */
57     // super(message);
58
// initCause(e);
59
/* #endif */
60   }
61
62   /**
63    * Return the embedded exception, if any.
64    *
65    * @return The embedded exception, or null if there is none.
66    */

67   public Throwable JavaDoc getException ()
68   {
69     return getCause();
70   }
71
72   /**
73    * Convert this exception to a string.
74    *
75    * @return A string version of this exception.
76    */

77   public String JavaDoc toString ()
78   {
79     return getMessage();
80   }
81
82   // The initCause/getCause functionality was added in JDK 1.4.
83
/* #ifndef use:java.lang.Throwable.getCause */
84   // public Throwable initCause(Throwable cause)
85
// {
86
// exception = cause;
87
// return this;
88
// }
89

90   // public Throwable getCause()
91
// {
92
// return exception;
93
// }
94

95   // private Throwable exception;
96
/* #endif */
97
98   /** Coerce argument to a RuntimeException. */
99   public static RuntimeException JavaDoc wrapIfNeeded (Throwable JavaDoc ex)
100   {
101     if (ex instanceof RuntimeException JavaDoc)
102       return (RuntimeException JavaDoc) ex;
103     else
104       return new WrappedException(ex);
105   }
106 }
107
Popular Tags