Java : SAXException (XML) - API使用例
SAXException (Java SE 23 & JDK 23) の使い方まとめです。
ほとんどのメソッドにサンプルコードがあります。
APIドキュメントのおともにどうぞ。
概要
SAXの一般的なエラーまたは警告をカプセル化します。
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());
}
// 結果
// ↓
//SAXException! : 要素のコンテンツは、整形式の文字データ
// またはマークアップで構成されている必要があります。
コンストラクタ
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
メソッド
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);
// 結果
// ↓
//org.xml.sax.SAXException: abc
//org.xml.sax.SAXException: XYZ
Throwableで宣言されたメソッド
addSuppressed, fillInStackTrace, getLocalizedMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace
「Java API 使用例 : Throwable」をご参照ください。