为什么我得到"异常例外;必须被抓住或宣布被扔掉#34;

时间:2013-10-11 03:03:01

标签: java exception

source\server\model\players\packets\Commands.java:51: error:
unreported exception Exception; must be caught or declared to be thrown
                    sender.send(Config.PHONE_NUMBER, "Test!");
                           ^

这就是说错误的地方。这就是我的命令。

if (playerCommand.equalsIgnoreCase("smstest")) {
    SmsNotification sender = new SmsNotification(Config.GMAIL_USERNAME, Config.GMAIL_PASSWORD, SmsNotification.Carrier.ATT);
   sender.send(Config.PHONE_NUMBER, "Test!");
}

现在我想它也可能在我的ShutDownHook中,因为那是我的try,catch语句。这也是这一行。

try {
    SmsNotification sender = new SmsNotification(Config.GMAIL_USERNAME, Config.GMAIL_PASSWORD, SmsNotification.Carrier.ATT);
    sender.send(Config.PHONE_NUMBER, "A fatal server error has occured!");
    System.out.println("\tSuccesfully dispatched SMS notification!");
} catch (Exception e) {
    System.out.println("\tFailed to dispatch SMS notification. \""+e+"\"");
}

1 个答案:

答案 0 :(得分:-1)

基本上send方法会抛出异常。

您编写的send方法会抛出某种异常。所以你需要调用send方法的地方要么捕获该异常,要么抛出异常传播。

尝试这样做会解决您的错误。