1. // SAX exception class.
  2. // No warranty; no copyright -- use this as you will.
  3. // $Id: SAXException.java,v 1.2 2001/08/01 06:43:18 tcng Exp $
  4. package org.xml.sax;
  5. /**
  6. * Encapsulate a general SAX error or warning.
  7. *
  8. * <blockquote>
  9. * <em>This module, both source code and documentation, is in the
  10. * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
  11. * </blockquote>
  12. *
  13. * <p>This class can contain basic error or warning information from
  14. * either the XML parser or the application: a parser writer or
  15. * application writer can subclass it to provide additional
  16. * functionality. SAX handlers may throw this exception or
  17. * any exception subclassed from it.</p>
  18. *
  19. * <p>If the application needs to pass through other types of
  20. * exceptions, it must wrap those exceptions in a SAXException
  21. * or an exception derived from a SAXException.</p>
  22. *
  23. * <p>If the parser or application needs to include information about a
  24. * specific location in an XML document, it should use the
  25. * {@link org.xml.sax.SAXParseException SAXParseException} subclass.</p>
  26. *
  27. * @since SAX 1.0
  28. * @author David Megginson,
  29. * <a href="mailto:sax@megginson.com">sax@megginson.com</a>
  30. * @version 2.0
  31. * @see org.xml.sax.SAXParseException
  32. */
  33. public class SAXException extends Exception {
  34. /**
  35. * Create a new SAXException.
  36. *
  37. * @param message The error or warning message.
  38. * @see org.xml.sax.Parser#setLocale
  39. */
  40. public SAXException (String message) {
  41. super(message);
  42. this.exception = null;
  43. }
  44. /**
  45. * Create a new SAXException wrapping an existing exception.
  46. *
  47. * <p>The existing exception will be embedded in the new
  48. * one, and its message will become the default message for
  49. * the SAXException.</p>
  50. *
  51. * @param e The exception to be wrapped in a SAXException.
  52. */
  53. public SAXException (Exception e)
  54. {
  55. super();
  56. this.exception = e;
  57. }
  58. /**
  59. * Create a new SAXException from an existing exception.
  60. *
  61. * <p>The existing exception will be embedded in the new
  62. * one, but the new exception will have its own message.</p>
  63. *
  64. * @param message The detail message.
  65. * @param e The exception to be wrapped in a SAXException.
  66. * @see org.xml.sax.Parser#setLocale
  67. */
  68. public SAXException (String message, Exception e)
  69. {
  70. super(message);
  71. this.exception = e;
  72. }
  73. /**
  74. * Return a detail message for this exception.
  75. *
  76. * <p>If there is an embedded exception, and if the SAXException
  77. * has no detail message of its own, this method will return
  78. * the detail message from the embedded exception.</p>
  79. *
  80. * @return The error or warning message.
  81. * @see org.xml.sax.Parser#setLocale
  82. */
  83. public String getMessage ()
  84. {
  85. String message = super.getMessage();
  86. if (message == null && exception != null) {
  87. return exception.getMessage();
  88. } else {
  89. return message;
  90. }
  91. }
  92. /**
  93. * Return the embedded exception, if any.
  94. *
  95. * @return The embedded exception, or null if there is none.
  96. */
  97. public Exception getException ()
  98. {
  99. return exception;
  100. }
  101. /**
  102. * Override toString to pick up any embedded exception.
  103. *
  104. * @return A string representation of this exception.
  105. */
  106. public String toString ()
  107. {
  108. if (exception != null) {
  109. return exception.toString();
  110. } else {
  111. return super.toString();
  112. }
  113. }
  114. //////////////////////////////////////////////////////////////////////
  115. // Internal state.
  116. //////////////////////////////////////////////////////////////////////
  117. /**
  118. * @serial The embedded exception if tunnelling, or null.
  119. */
  120. private Exception exception;
  121. }
  122. // end of SAXException.java