未报告的例外例外;必须被抓住或宣布被抛出

时间:2013-11-18 04:16:41

标签: java

有两个类: 主要的一个(Corina.java)和我遇到的问题(Functions.java)。在没有太多复杂化的情况下,Corina.java调用Functions.java中的方法,该方法会检查booleantrue还是false并根据该请求进行身份验证,此代码非常公正,尽管我使用的是一个小部件RFID阅读器,并复制了他们的一个例子。但我在JCreator中收到以下错误:

--------------------Configuration: Corina - JDK version 1.7.0_45 <Default> - <Default>--------------------
C:\Users\alexis.JKLSEMICOLON\Documents\JCreator LE\MyProjects\Corina\src\Functions.java:23: error: unreported exception Exception; must be caught or declared to be thrown
            authenticateContinue();

                            ^

头等代码:

public class Corina {

    public static void main(String[] args) {
        Functions funtion = new Functions();
        funtion.authenticateStart();
    }
}

第二类代码:

import com.phidgets.*;
import com.phidgets.event.*;

public class Functions {

    public void authenticateStart() {
        boolean authStatus = false;
        System.out.println("The authentication status is currently: " + authStatus + ".");
        if (authStatus) {
            System.out.println("The applications is unlocked. Please wait.");
            //  applicationStart();
        } else {
            System.out.println("Please authenticate now by swiping one of the RFID tags allowed to unlock the program.");
            authenticateContinue();
        }

    }

    public void authenticateContinue() throws Exception {
        RFIDPhidget rfid;

        System.out.println(Phidget.getLibraryVersion());

        rfid = new RFIDPhidget();
        rfid.addAttachListener(new AttachListener() {
            public void attached(AttachEvent ae) {
                try {

                    ((RFIDPhidget) ae.getSource()).setAntennaOn(true);

                    ((RFIDPhidget) ae.getSource()).setLEDOn(true);
                } catch (PhidgetException ex) {
                }
                System.out.println("attachment of " + ae);
            }
        });
        rfid.addDetachListener(new DetachListener() {
            public void detached(DetachEvent ae) {
                System.out.println("detachment of " + ae);
            }
        });
        rfid.addErrorListener(new ErrorListener() {
            public void error(ErrorEvent ee) {
                System.out.println("error event for " + ee);
            }
        });
        rfid.addTagGainListener(new TagGainListener() {
            public void tagGained(TagGainEvent oe) {
                System.out.println(oe);
            }
        });
        rfid.addTagLossListener(new TagLossListener() {
            public void tagLost(TagLossEvent oe) {
                System.out.println(oe);
            }
        });
        rfid.addOutputChangeListener(new OutputChangeListener() {
            public void outputChanged(OutputChangeEvent oe) {
                System.out.println(oe);
            }
        });

        rfid.openAny();
        System.out.println("waiting for RFID attachment...");
        rfid.waitForAttachment(1000);

        System.out.println("Serial: " + rfid.getSerialNumber());
        System.out.println("Outputs: " + rfid.getOutputCount());

        System.out.println("Outputting events. Input to stop.");
        System.in.read();
        System.out.print("closing...");
        rfid.close();
        rfid = null;
        System.out.println(" ok");
        if (false) {
            System.out.println("wait for finalization...");
            System.gc();
        }
    }
}

任何帮助将不胜感激。理想情况下,我想将标签保存为字符串,所以如果您对此有所了解,请务必使用。

1 个答案:

答案 0 :(得分:3)

您的问题是authenticateStart来电authenticateContinue(),但您已标记authenticateContinue能够投出Exception。这意味着authenticateStart需要能够在抛出异常时处理该异常。你有几个选择。

  1. 将来自authenticateContinue的来电置于try区块内,并在其下方的catch区块中处理该异常。
  2. 更改authenticateContinue,以便它不会抛出已检查的异常。
  3. 标记authenticateStart可以抛出Exception。这会将问题提升到main,您可以在其中调用authenticateStart
  4. 无论你做什么,你都必须以某种方式处理这个例外。 Java异常处理的重点在于,您不能将未经处理的异常处理 - 您必须以某种方式处理它们。