View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.jxpath;
18  
19  /**
20   * Thrown in various situations by JXPath; may contain a nested exception.
21   *
22   * @author Dmitri Plotnikov
23   * @version $Revision: 618149 $ $Date: 2008-02-04 03:04:13 +0100 (Mo, 04 Feb 2008) $
24   */
25  
26  public class JXPathException extends RuntimeException {
27      private static final long serialVersionUID = 4306409701468017766L;
28  
29      /** @serial */
30      private Throwable exception;
31  
32      /**
33       * Create a new <code>JXPathException</code> with no
34       * detail mesage.
35       */
36  
37       public JXPathException() {
38           super();
39           this.exception = null;
40       }
41  
42      /**
43       * Create a new <code>JXPathException</code> with
44       * the <code>String </code> specified as an error message.
45       *
46       * @param msg The error message for the exception.
47       */
48      public JXPathException(String msg) {
49          super(msg);
50          this.exception = null;
51      }
52  
53  
54      /**
55       * Create a new <code>JXPathException</code> with a
56       * given <code>Throwable</code> base cause of the error.
57       *
58       * @param e The exception to be encapsulated in a
59       * JXPathException.
60       */
61      public JXPathException(Throwable e) {
62          super(e.toString());
63          this.exception = e;
64      }
65  
66      /**
67       * Create a new <code>JXPathException</code> with the
68       * given <code>Exception</code> base cause and detail message.
69       *
70       * @param msg The detail message.
71       * @param e The exception to be encapsulated in a JXPathException
72       */
73      public JXPathException(String msg, Throwable e) {
74          super(msg);
75          this.exception = e;
76      }
77  
78  
79      /**
80       * Return the message (if any) for this error . If there is no
81       * message for the exception and there is an encapsulated
82       * exception then the message of that exception will be returned.
83       *
84       * @return The error message.
85       */
86      public String getMessage() {
87          String message = super.getMessage();
88          if (exception == null) {
89              return message;
90          }
91          StringBuffer buf = new StringBuffer();
92          if (message != null) {
93              buf.append(message).append("; ");
94          }
95          String eMsg = exception.getMessage();
96          buf.append(eMsg == null ? exception.getClass().getName() : eMsg);
97          return buf.toString();
98      }
99  
100     /**
101      * Return the actual exception (if any) that caused this exception to
102      * be raised.
103      *
104      * @return The encapsulated exception, or null if there is none.
105      */
106     public Throwable getException() {
107         return exception;
108     }
109 
110     /**
111      * Same as {@link #getException() getException()}
112      * @return The encapsulated exception, or null if there is none.
113      */
114     public Throwable getCause() {
115         return exception;
116     }
117 
118 }