有两个类:
主要的一个(Corina.java
)和我遇到的问题(Functions.java
)。在没有太多复杂化的情况下,Corina.java
调用Functions.java
中的方法,该方法会检查boolean
是true
还是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();
}
}
}
任何帮助将不胜感激。理想情况下,我想将标签保存为字符串,所以如果您对此有所了解,请务必使用。
答案 0 :(得分:3)
您的问题是authenticateStart
来电authenticateContinue()
,但您已标记authenticateContinue
能够投出Exception
。这意味着authenticateStart
需要能够在抛出异常时处理该异常。你有几个选择。
authenticateContinue
的来电置于try
区块内,并在其下方的catch
区块中处理该异常。authenticateContinue
,以便它不会抛出已检查的异常。authenticateStart
可以抛出Exception
。这会将问题提升到main
,您可以在其中调用authenticateStart
。无论你做什么,你都必须以某种方式处理这个例外。 Java异常处理的重点在于,您不能将未经处理的异常处理 - 您必须以某种方式处理它们。