Capturing an exception from Xalan
I have a Java program (running in JDK 1.5 for now) which is getting a strange exception while processing an XSLT stylesheet using Xalan. I'm not looking for how to fix the exception: there's plenty of information online about that. I just want to know how to capture the exception in my code:
try {
TransformerFactory tf = TransformerFactory.newInstance();
Source src = new SAXSource(new InputSource(new FileInputStream("doc.xsl")));
Transformer t = tf.newTransformer(src);
System.out.println(t);
} catch (TransformerConfigurationException e) {
System.out.println("the exception was " + e + " and its cause is " + e.getCause());
}
and the output:
com.sun.org.apache.bcel.internal.generic.ClassGenException: Branch target offset too large for short
at com.sun.org.apache.bcel.internal.generic.BranchInstruction.dump(BranchInstruction.java:99)
at com.sun.org.apache.bcel.internal.generic.InstructionList.getByteCode(InstructionList.java:980)
at com.sun.org.apache.bcel.internal.generic.MethodGen.getMethod(MethodGen.java:616)
at com.sun.org.apache.xalan.internal.xsltc.compiler.Mode.compileNamedTemplate(Mode.java:556)
at com.sun.org.apache.xalan.internal.xsltc.compiler.Mode.compileTemplates(Mode.java:566)
at com.sun.org.apache.xalan.internal.xsltc.compiler.Mode.compileApplyTemplates(Mode.java:818)
at com.sun.org.apache.xalan.internal.xsltc.compiler.Stylesheet.compileModes(Stylesheet.java:615)
at com.sun.org.apache.xalan.internal.xsltc.compiler.Stylesheet.translate(Stylesheet.java:730)
at com.sun.org开发者_JAVA技巧.apache.xalan.internal.xsltc.compiler.XSLTC.compile(XSLTC.java:354)
at com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.compile(XSLTC.java:429)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:792)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:614)
at main.Main.main(Main.java:61)
ERROR: 'Branch target offset too large for short'
FATAL ERROR: 'Could not compile stylesheet'
the exception was javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet and its cause is null
What I want to do is capture the inner exception - the ClassGenException
- inside my code. Simply having it printed to STDERR as above is not useful in my application. Is there a way of doing that?
Did you try to set an ErrorListener on your TransformerFactory?
tf.setErrorListener(new ErrorListener() {
@Override
public void warning(TransformerException exception) throws TransformerException {
...
}
@Override
public void fatalError(TransformerException exception) throws TransformerException {
...
}
@Override
public void error(TransformerException exception) throws TransformerException {
...
}
});
Your ClassGenException might be available via exception.getCause()
.
精彩评论