简体   繁体   English

Java编译器正在打印未报告的异常XPathExpressionExeption

[英]Java compiler is printing unreported exception XPathExpressionExeption

I think I have found the answer I was looking for: I think I have found a solution, to cut down on the amount of code and readability. 我想我已经找到了想要的答案:我想我已经找到了一种解决方案,可以减少代码量和可读性。 I have declared the variable at the start: 我在一开始就声明了变量:

XPathExpression newExpression;

Then created a method I can call: 然后创建一个我可以调用的方法:

XPathExpression exprNS = exprNSCreate("/LVOBSLSTR/NameSpace");

public XPathExpression exprNSCreate(String pathToCompile) {
                try {
                        newExpression = xpath.compile(pathToCompile);
                } catch (Exception e) {
                        System.out.println("XPathExpressionException Error " +     e.getMessage());
                        e.printStackTrace();
                }
                return newExpression;
        }
enter code here

I have not used java in many years, I have been writing this piece of code, it is far for complete. 我已经多年没有使用Java了,我一直在写这段代码,到目前为止还远远没有完成。 What it is going to do is change the outgoing and incoming XML to our system. 它要做的是将传出和传入XML更改为我们的系统。 The reason for this is that the system is written through a rules engine and there are some limitations on variable lengths so we are fixing some things outside of the system. 这样做的原因是系统是通过规则引擎编写的,并且变量长度存在一些限制,因此我们正在系统外修复某些问题。

I cannot find a reference to my problem anywhere. 我在任何地方都找不到对我的问题的参考。 The issue is that the java compiler is complaining about a unreported exception, but I cannot see any examples declaring this. 问题是Java编译器抱怨未报告的异常,但是我看不到任何声明此异常的示例。

If I change the code to try/catch the problem goes away, but I do not want to do try/catch for each xpath expression. 如果我更改代码以尝试/捕获,则问题会消失,但我不想为每个xpath表达式进行尝试/捕获。

I am compiling on a red-hat linux machine. 我正在红帽Linux机器上进行编译。

Can someone please point me in the right direction? 有人可以指出正确的方向吗? Thanks in advance. 提前致谢。

This is the error I am getting: 这是我得到的错误:

LVOBSLSTR.java:58: unreported exception javax.xml.xpath.XPathExpressionException; must be caught or declared to be thrown
                XPathExpression exprNS = xpath.compile("/LVOBSLSTR/NameSpace");
                                                  ^
LVOBSLSTR.java:59: unreported exception javax.xml.xpath.XPathExpressionException; must be caught or declared to be thrown
                NodeList listNS = (NodeList) exprNS.evaluate(paramDoc, XPathConstants.NODESET);

This is the code: 这是代码:

/**
* The LVOBSLSTR class implements the OBSLSTR class  
* it applies XML data translation dependant on the party
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathConstants;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import java.util.ArrayList;

class LVOBSLSTR implements OBSLSTR  {
        Document paramDoc;
        ArrayList<Object> instructionList;
        Element nodeElement;
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();

        public boolean activate() {
                System.out.println("boolean activate");
                if(null==instructionList) {
                        System.out.println("!!! instructionList NULL creating one !!!");
                        instructionList = new ArrayList<Object>();
                        loadXMLparameterFile();
                        System.out.println("AFTER loadXMLparameterFile");
                }
                return true;
        }

        public void beforePost() {
                System.out.println("beforePost");
                OUTBSOAP.statMessageBuffer =     applyInstructions(OUTBSOAP.statMessageBuffer, "OutBound");
        }

        public void afterPost() {
                System.out.println("afterPost");
                OUTBSOAP.statReplyMessage = applyInstructions(OUTBSOAP.statReplyMessage,     "InBound");
        }

        public String applyInstructions(String xmlString, String inOrOut) {
                XPathExpression exprNS = xpath.compile("/LVOBSLSTR/NameSpace");
                NodeList listNS = (NodeList) exprNS.evaluate(paramDoc,     XPathConstants.NODESET);
                return xmlString;
        }


        public void loadXMLparameterFile() {
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

                try {
                        System.out.println("try");
                        DocumentBuilder builder = factory.newDocumentBuilder();
                        FileInputStream fis = new FileInputStream("LVOBSLSTR.xml");
                        InputSource is = new InputSource(fis);
                        paramDoc = builder.parse(is);
                } catch (Exception e) {
                        System.out.println("LVOBSLSTR loadXMLparameterFile Error " +     e.getMessage());
                        e.printStackTrace();
                }
        }
}

Basically exceptions come in two types: checked and unchecked exceptions. 基本上,异常有两种类型:已检查和未检查的异常。 The unchecked exceptions are thrown when a program error occurs such as null pointers, division by zero, illegal arguments, array index out of bounds etc. 当发生程序错误(例如空指针,零除,非法参数,数组索引超出范围等)时,将引发未经检查的异常。

The compiler can't check on these exceptions so it doesn't. 编译器无法检查这些异常,因此不会。 The checked exceptions are usually external events that cause the program to fail such as IO errors, broken socket connections etc. When a piece of code can throw a checked exception it has to say so in its method header otherwise the method can't throw such an exception. 受检查的异常通常是导致程序失败的外部事件,例如IO错误,套接字连接断开等。当一段代码可以引发受检查的异常时,必须在其方法标头中声明,否则该方法无法抛出该异常。一个例外。 It can catch such exceptions and deal with it (eg repair the situation, inform the user about it etc.) 它可以捕获此类异常并对其进行处理(例如,修复情况,将其告知用户等)。

The compiler checks your code for it: ie when your method can (in)directly throw an exception it has to report it in its method header: you either have to catch and handle them or report that your method lets that exception pass to its caller(s). 编译器检查它的代码:即,当您的方法可以(直接)抛出异常时,它必须在其方法标头中报告该异常:您要么必须捕获并处理它们,要么报告您的方法将该异常传递给其调用方(s)。 If you do neither the compiler will tell you that you forgot to report the exception. 如果您不这样做,编译器将告诉您您忘记报告该异常。 Reporting is simply adding a "throws SuchAndSoException" to the method declaration. 报告只是在方法声明中添加“引发SuchAndSoException”。 If you fail to do that the compiler will flag that as a compilation error. 如果操作失败,编译器会将其标记为编译错误。

So in your case above you will either need to declare each relevant method as "throws XPathExpressionException" or ideally add a try/catch block to each method and do something when an error occurs. 因此,在上述情况下,您要么需要将每个相关方法声明为“ throws XPathExpressionException”,要么理想地向每个方法添加一个try / catch块,并在发生错误时执行某些操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM