Java : SAXException (XML) with Examples

SAXException (Java SE 23 & JDK 23) in Java with Examples.
You will find code samples for most of the SAXException methods.


Summary

Encapsulate a general SAX error or warning.

Class diagram

final var xml = """
        <root><<<root>
        """;

final var factory = DocumentBuilderFactory.newInstance();
final var builder = factory.newDocumentBuilder();

try {
    var _ = builder.parse(new ByteArrayInputStream(xml.getBytes()));
} catch (SAXException e) {
    System.out.println("SAXException! : " + e.getMessage());
}

// Result
// ↓
//SAXException! : The content of elements must consist of
// well-formed character data or markup.

Constructors

SAXException ()

Create a new SAXException.

final var e = new SAXException();
System.out.println(e); // org.xml.sax.SAXException

SAXException (Exception e)

Create a new SAXException wrapping an existing exception.

final var cause = new SAXException("XYZ");
final var e = new SAXException(cause);

System.out.println(e); // org.xml.sax.SAXException: org.xml.sax.SAXException: XYZ

System.out.println(e.getException()); // org.xml.sax.SAXException: XYZ
System.out.println(e.getException().getMessage()); // XYZ

System.out.println(e.getCause()); // org.xml.sax.SAXException: XYZ
System.out.println(e.getCause().getMessage()); // XYZ

SAXException (String message)

Create a new SAXException.

final var e = new SAXException("abc");
System.out.println(e); // org.xml.sax.SAXException: abc
System.out.println(e.getMessage()); // abc

SAXException (String message, Exception e)

Create a new SAXException from an existing exception.

final var cause = new SAXException("XYZ");
final var e = new SAXException("abc", cause);

System.out.println(e.getMessage()); // abc

System.out.println(e.getException()); // org.xml.sax.SAXException: XYZ
System.out.println(e.getException().getMessage()); // XYZ

Methods

Throwable getCause ()

Return the cause of the exception

final var cause = new SAXException("XYZ");
final var e = new SAXException(cause);

System.out.println(e); // org.xml.sax.SAXException: org.xml.sax.SAXException: XYZ

System.out.println(e.getException()); // org.xml.sax.SAXException: XYZ
System.out.println(e.getException().getMessage()); // XYZ

System.out.println(e.getCause()); // org.xml.sax.SAXException: XYZ
System.out.println(e.getCause().getMessage()); // XYZ

Exception getException ()

Return the embedded exception, if any.

final var cause = new SAXException("XYZ");
final var e = new SAXException(cause);

System.out.println(e); // org.xml.sax.SAXException: org.xml.sax.SAXException: XYZ

System.out.println(e.getException()); // org.xml.sax.SAXException: XYZ
System.out.println(e.getException().getMessage()); // XYZ

System.out.println(e.getCause()); // org.xml.sax.SAXException: XYZ
System.out.println(e.getCause().getMessage()); // XYZ

String getMessage ()

Return a detail message for this exception.

final var e = new SAXException("abc");
System.out.println(e); // org.xml.sax.SAXException: abc
System.out.println(e.getMessage()); // abc

String toString ()

Override toString to pick up any embedded exception.

final var cause = new SAXException("XYZ");
final var e = new SAXException("abc", cause);

final var str = e.toString();
System.out.println(str);

// Result
// ↓
//org.xml.sax.SAXException: abc
//org.xml.sax.SAXException: XYZ

Methods declared in Throwable

addSuppressed, fillInStackTrace, getLocalizedMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace

Please see the link below.


Related posts

To top of page