1. /*
  2. * Copyright 1999-2004 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. package org.apache.commons.jxpath;
  17. /**
  18. * Thrown in various situations by JXPath; may contain a nested exception.
  19. *
  20. * @author Dmitri Plotnikov
  21. * @version $Revision: 1.5 $ $Date: 2004/02/29 14:17:42 $
  22. */
  23. public class JXPathException extends RuntimeException {
  24. /** @serial */
  25. private Throwable exception;
  26. /**
  27. * Create a new <code>JXPathException</code> with no
  28. * detail mesage.
  29. */
  30. public JXPathException() {
  31. super();
  32. this.exception = null;
  33. }
  34. /**
  35. * Create a new <code>JXPathException</code> with
  36. * the <code>String </code> specified as an error message.
  37. *
  38. * @param msg The error message for the exception.
  39. */
  40. public JXPathException(String msg) {
  41. super(msg);
  42. this.exception = null;
  43. }
  44. /**
  45. * Create a new <code>JXPathException</code> with a
  46. * given <code>Throwable</code> base cause of the error.
  47. *
  48. * @param e The exception to be encapsulated in a
  49. * JXPathException.
  50. */
  51. public JXPathException(Throwable e) {
  52. super(e.toString());
  53. this.exception = e;
  54. }
  55. /**
  56. * Create a new <code>JXPathException</code> with the
  57. * given <code>Exception</code> base cause and detail message.
  58. *
  59. * @param e The exception to be encapsulated in a
  60. * JXPathException
  61. * @param msg The detail message.
  62. */
  63. public JXPathException(String msg, Throwable e) {
  64. super(msg);
  65. this.exception = e;
  66. }
  67. /**
  68. * Return the message (if any) for this error . If there is no
  69. * message for the exception and there is an encapsulated
  70. * exception then the message of that exception will be returned.
  71. *
  72. * @return The error message.
  73. */
  74. public String getMessage() {
  75. String message = super.getMessage();
  76. if (exception != null) {
  77. if (message == null) {
  78. if (exception.getMessage() != null) {
  79. return exception.getMessage();
  80. }
  81. else {
  82. return exception.getClass().getName();
  83. }
  84. }
  85. else {
  86. if (exception.getMessage() != null) {
  87. return message + "; " + exception.getMessage();
  88. }
  89. else {
  90. return message + "; " + exception.getClass().getName();
  91. }
  92. }
  93. }
  94. return message;
  95. }
  96. /**
  97. * Return the actual exception (if any) that caused this exception to
  98. * be raised.
  99. *
  100. * @return The encapsulated exception, or null if there is none.
  101. */
  102. public Throwable getException() {
  103. return exception;
  104. }
  105. }