Java : SAXException (XML) con ejemplos
SAXException (Java SE 23 & JDK 23) en Java con ejemplos.
Encontrará muestras de código para la mayoría de los métodos SAXException.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
Encapsular un error o advertencia general de SAX. (Traducción automática)
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 ()
Crear una nueva SAXException. (Traducción automática)
final var e = new SAXException();
System.out.println(e); // org.xml.sax.SAXException
SAXException (Exception e)
Crea una nueva SAXException que envuelva una excepción existente. (Traducción automática)
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)
Crear una nueva SAXException. (Traducción automática)
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)
Crea una nueva SAXException a partir de una excepción existente. (Traducción automática)
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 ()
Devuelve la causa de la excepción (Traducción automática)
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 ()
Devuelve la excepción incorporada, si la hay. (Traducción automática)
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 ()
Devuelve un mensaje detallado para esta excepción. (Traducción automática)
final var e = new SAXException("abc");
System.out.println(e); // org.xml.sax.SAXException: abc
System.out.println(e.getMessage()); // abc
String toString ()
Anule toString para detectar cualquier excepción incorporada. (Traducción automática)
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
Consulte el siguiente enlace.