Java : TransformerConfigurationException (XML) with Examples

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


Summary

Indicates a serious configuration error.

Class diagram

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

// Result
// ↓
//javax.xml.transform.TransformerConfigurationException:
// Cannot set the feature 'dummy' on this TransformerFactory.

Constructors

TransformerConfigurationException ()

Create a new TransformerConfigurationException with no detail message.

final var e = new TransformerConfigurationException();
System.out.println(e); // javax.xml.transform.TransformerConfigurationException: Configuration Error

TransformerConfigurationException (String msg)

Create a new TransformerConfigurationException with the String specified as an error message.

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

TransformerConfigurationException (String msg, Throwable e)

Create a new TransformerConfigurationException with the given Exception base cause and detail message.

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

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

TransformerConfigurationException (String message, SourceLocator locator)

Create a new TransformerConfigurationException 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 TransformerConfigurationException("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());

TransformerConfigurationException (String message, SourceLocator locator, Throwable e)

Wrap an existing exception in a TransformerConfigurationException.

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 TransformerConfigurationException("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());

TransformerConfigurationException (Throwable e)

Create a new TransformerConfigurationException with a given Exception base cause of the error.

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

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

Methods declared in TransformerException

getCause, getException, getLocationAsString, getLocator, getMessageAndLocation, initCause, printStackTrace, printStackTrace, printStackTrace, setLocator

Please see the link below.

Methods declared in Throwable

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

Please see the link below.


Related posts

To top of page