首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未报告的异常;必须捕获或声明为引发

未报告的异常;必须捕获或声明为引发
EN

Stack Overflow用户
提问于 2013-11-18 04:16:41
回答 1查看 8.7K关注 0票数 1

有两个类:主类(Corina.java)和我有问题的类(Functions.java)。在不太复杂的情况下,Corina.javaFunctions.java中调用了一个方法,该方法检查booleantrue还是false,并在此基础上请求身份验证,此时代码非常公正,尽管我使用的是phidgets阅读器,并复制了其中一部分示例。但是,在JCreator中我得到了以下错误:

代码语言:javascript
复制
--------------------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();

                            ^

一级代码:

代码语言:javascript
复制
public class Corina {

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

二级代码:

代码语言:javascript
复制
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();
        }
    }
}

任何帮助都将不胜感激。理想情况下,我希望将标记保存到字符串中,因此,如果您对此有所了解的话,请尽一切努力。

EN

回答 1

Stack Overflow用户

发布于 2013-11-18 04:29:10

您的问题是authenticateStart调用了authenticateContinue(),但是您已经将authenticateContinue标记为能够抛出Exception。这意味着当抛出异常时,authenticateStart需要能够处理该异常。你有几个选择。

  1. 将对authenticateContinue的调用放在try块中,并在其下面的catch块中处理异常。
  2. 更改authenticateContinue,使其不会抛出已检查的异常。
  3. authenticateStart标记为能够抛出Exception。这将把问题推到main中,您在那里调用authenticateStart

不管你做什么,你都得设法处理这个例外。Java异常处理的全部要点是,您不能只是不处理检查过的异常--您必须以某种方式处理它们。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20039991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档