gpt4 book ai didi

java - 从 Xalan 捕获异常

转载 作者:行者123 更新时间:2023-12-04 06:42:00 28 4
gpt4 key购买 nike

我有一个 Java 程序(目前在 JDK 1.5 中运行),它在使用 Xalan 处理 XSLT 样式表时出现奇怪的异常。我不是在寻找如何解决异常:网上有很多关于这个的信息。我只想知道如何在我的代码中捕获异常:

    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());
}

和输出:
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.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

我想要做的是捕获内部异常 - ClassGenException - 在我的代码中。像上面那样简单地将它打印到 STDERR 在我的应用程序中没有用。有没有办法做到这一点?

最佳答案

您是否尝试在 TransformerFactory 上设置 ErrorListener ?

    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 {
...

}
});

您的 ClassGenException 可能通过 exception.getCause() 获得.

关于java - 从 Xalan 捕获异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4135638/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com