Java : TransformerException (XML) with Examples

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


Summary

This class specifies an exceptional condition that occurred during the transformation process.

Class diagram

try {
    final var factory = TransformerFactory.newInstance();
    factory.setFeature("dummy", true);
} catch (TransformerException e) {
    System.out.println("TransformerException! : " + e.getMessage());
}

// Result
// ↓
//TransformerException! : Cannot set the feature 'dummy' on this TransformerFactory.

Constructors

TransformerException (String message)

Create a new TransformerException.

final var e = new TransformerException("abc");
System.out.println(e); // javax.xml.transform.TransformerException: abc

TransformerException (String message, Throwable e)

Wrap an existing exception in a TransformerException.

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

System.out.println(e); // javax.xml.transform.TransformerException: abc
System.out.println(e.getCause()); // java.io.IOException: XYZ

TransformerException (String message, SourceLocator locator)

Create a new TransformerException from a message and a Locator.

final var locator = new SourceLocator() {
    @Override
    public String getPublicId() {
        return "public id!";
    }

    @Override
    public String getSystemId() {
        return "system id!";
    }

    @Override
    public int getLineNumber() {
        return 123;
    }

    @Override
    public int getColumnNumber() {
        return 456;
    }
};
final var e = new TransformerException("abc", locator);
System.out.println(e.getLocator() == locator); // true

// ; SystemID: system id!; Line#: 123; Column#: 456
System.out.println(e.getLocationAsString());

// abc; SystemID: system id!; Line#: 123; Column#: 456
System.out.println(e.getMessageAndLocation());

TransformerException (String message, SourceLocator locator, Throwable e)

Wrap an existing exception in a TransformerException.

final var locator = new SourceLocator() {
    @Override
    public String getPublicId() {
        return "public id!";
    }

    @Override
    public String getSystemId() {
        return "system id!";
    }

    @Override
    public int getLineNumber() {
        return 123;
    }

    @Override
    public int getColumnNumber() {
        return 456;
    }
};
final var cause = new IOException("XYZ");
final var e = new TransformerException("abc", locator, cause);

System.out.println(e.getLocator() == locator); // true
System.out.println(e.getCause()); // java.io.IOException: XYZ

// ; SystemID: system id!; Line#: 123; Column#: 456
System.out.println(e.getLocationAsString());

// abc; SystemID: system id!; Line#: 123; Column#: 456
System.out.println(e.getMessageAndLocation());

TransformerException (Throwable e)

Create a new TransformerException wrapping an existing exception.

final var cause = new IOException("XYZ");
final var e = new TransformerException(cause);

System.out.println(e); // javax.xml.transform.TransformerException: java.io.IOException: XYZ
System.out.println(e.getCause()); // java.io.IOException: XYZ
System.out.println(e.getException()); // java.io.IOException: XYZ

Methods

Throwable getCause ()

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

final var cause = new IOException("XYZ");
final var e = new TransformerException(cause);

System.out.println(e); // javax.xml.transform.TransformerException: java.io.IOException: XYZ
System.out.println(e.getCause()); // java.io.IOException: XYZ
System.out.println(e.getException()); // java.io.IOException: XYZ

Throwable getException ()

This method retrieves an exception that this exception wraps.

final var cause = new IOException("XYZ");
final var e = new TransformerException(cause);

System.out.println(e); // javax.xml.transform.TransformerException: java.io.IOException: XYZ
System.out.println(e.getCause()); // java.io.IOException: XYZ
System.out.println(e.getException()); // java.io.IOException: XYZ

String getLocationAsString ()

Get the location information as a string.

final var locator = new SourceLocator() {
    @Override
    public String getPublicId() {
        return "public id!";
    }

    @Override
    public String getSystemId() {
        return "system id!";
    }

    @Override
    public int getLineNumber() {
        return 123;
    }

    @Override
    public int getColumnNumber() {
        return 456;
    }
};
final var e = new TransformerException("abc", locator);
System.out.println(e.getLocator() == locator); // true

// ; SystemID: system id!; Line#: 123; Column#: 456
System.out.println(e.getLocationAsString());

// abc; SystemID: system id!; Line#: 123; Column#: 456
System.out.println(e.getMessageAndLocation());

SourceLocator getLocator ()

Method getLocator retrieves an instance of a SourceLocator object that specifies where an error occurred.

final var locator = new SourceLocator() {
    @Override
    public String getPublicId() {
        return "public id!";
    }

    @Override
    public String getSystemId() {
        return "system id!";
    }

    @Override
    public int getLineNumber() {
        return 123;
    }

    @Override
    public int getColumnNumber() {
        return 456;
    }
};
final var e = new TransformerException("abc", locator);
System.out.println(e.getLocator() == locator); // true

// ; SystemID: system id!; Line#: 123; Column#: 456
System.out.println(e.getLocationAsString());

// abc; SystemID: system id!; Line#: 123; Column#: 456
System.out.println(e.getMessageAndLocation());

String getMessageAndLocation ()

Get the error message with location information appended.

final var locator = new SourceLocator() {
    @Override
    public String getPublicId() {
        return "public id!";
    }

    @Override
    public String getSystemId() {
        return "system id!";
    }

    @Override
    public int getLineNumber() {
        return 123;
    }

    @Override
    public int getColumnNumber() {
        return 456;
    }
};
final var e = new TransformerException("abc", locator);
System.out.println(e.getLocator() == locator); // true

// ; SystemID: system id!; Line#: 123; Column#: 456
System.out.println(e.getLocationAsString());

// abc; SystemID: system id!; Line#: 123; Column#: 456
System.out.println(e.getMessageAndLocation());

Throwable initCause (Throwable cause)

Initializes the cause of this throwable to the specified value.

final var e = new TransformerException("abc");
System.out.println(e.getCause()); // null
System.out.println(e.getException()); // null

final var cause = new IOException("XYZ");
e.initCause(cause);
System.out.println(e.getCause()); // java.io.IOException: XYZ
System.out.println(e.getException()); // java.io.IOException: XYZ

void printStackTrace ()

Print the trace of methods from where the error originated.

public class Main {

    public static void main(String[] args) {
        try {
            func1();
        } catch (TransformerException e) {
            e.printStackTrace();

            // Result
            // ↓
            // javax.xml.transform.TransformerException: abc
            //   Main.func2 ...
            //   Main.func1 ...
            //   Main.main ...
            // ...
        }
    }

    private static void func1() throws TransformerException {
        func2();
    }

    private static void func2() throws TransformerException {
        throw new TransformerException("abc");
    }
}

void printStackTrace (PrintStream s)

Print the trace of methods from where the error originated.

public class Main {

    public static void main(String[] args) {
        try {
            func1();
        } catch (TransformerException e) {
            System.out.println("-- PrintStream --");
            e.printStackTrace(System.out);

            // Result
            // ↓
            //-- PrintStream --
            // javax.xml.transform.TransformerException: abc
            //   Main.func2 ...
            //   Main.func1 ...
            //   Main.main ...
            // ...
        }
    }

    private static void func1() throws TransformerException {
        func2();
    }

    private static void func2() throws TransformerException {
        throw new TransformerException("abc");
    }
}

void printStackTrace (PrintWriter s)

Print the trace of methods from where the error originated.

public class Main {

    public static void main(String[] args) {
        try {
            func1();
        } catch (TransformerException e) {
            final var stringWriter = new StringWriter();
            try (final var printWriter = new PrintWriter(stringWriter)) {
                e.printStackTrace(printWriter);
            }

            System.out.println("-- PrintWriter --");
            System.out.println(stringWriter);

            // Result
            // ↓
            //-- PrintWriter --
            // javax.xml.transform.TransformerException: abc
            //   Main.func2 ...
            //   Main.func1 ...
            //   Main.main ...
            // ...
        }
    }

    private static void func1() throws TransformerException {
        func2();
    }

    private static void func2() throws TransformerException {
        throw new TransformerException("abc");
    }
}

void setLocator (SourceLocator location)

Method setLocator sets an instance of a SourceLocator object that specifies where an error occurred.

final var locator = new SourceLocator() {
    @Override
    public String getPublicId() {
        return "public id!";
    }

    @Override
    public String getSystemId() {
        return "system id!";
    }

    @Override
    public int getLineNumber() {
        return 123;
    }

    @Override
    public int getColumnNumber() {
        return 456;
    }
};
final var e = new TransformerException("abc");

// null
System.out.println(e.getLocationAsString());

e.setLocator(locator);

// abc; SystemID: system id!; Line#: 123; Column#: 456
System.out.println(e.getLocationAsString());

Methods declared in Throwable

addSuppressed, fillInStackTrace, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, setStackTrace, toString

Please see the link below.


Related posts

To top of page