Java : SAXException (XML) 示例
Java 中的 SAXException (Java SE 23 & JDK 23) 及其示例。
您将找到大多数 SAXException 方法的代码示例。
注解 :
- 本文可能使用了翻译软件以方便阅读。 另请查看英文原文。
简介
封装一般的 SAX 错误或警告。 (机器翻译)
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 ()
创建一个新的SAXException。 (机器翻译)
final var e = new SAXException();
System.out.println(e); // org.xml.sax.SAXException
SAXException (Exception e)
创建一个包装现有异常的新 SAXException。 (机器翻译)
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)
创建一个新的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)
从现有异常创建一个新的 SAXException。 (机器翻译)
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 ()
返回异常的原因 (机器翻译)
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 ()
如果有的话,返回嵌入的异常。 (机器翻译)
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 ()
返回此异常的详细消息。 (机器翻译)
final var e = new SAXException("abc");
System.out.println(e); // org.xml.sax.SAXException: abc
System.out.println(e.getMessage()); // abc
String toString ()
覆盖 toString 来获取任何嵌入的异常。 (机器翻译)
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
请参阅下面的链接。