简体   繁体   中英

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. What it is going to do is change the outgoing and incoming XML to our system. 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.

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.

I am compiling on a red-hat linux machine.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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