【问题标题】:Unreported exception ...... must be caught or declared to be thrown; despite *thrown* keyword未报告的异常......必须被捕获或声明被抛出;尽管 *throw* 关键字
【发布时间】:2014-11-24 15:56:15
【问题描述】:

我有一个方法被定义为:

private void startUSSDNotification(SimpleReference reference,
                                   String ussdServiceActivationNumber,
                                   String criteria)
    throws PolicyException {
    // Note that the injected javax.xml.ws.Service reference as well as port objects are not thread safe.
    // If the calling of port operations may lead to race condition some synchronization is required.
    USSDNotificationManager port = service.getUSSDNotificationManager();
    port.startUSSDNotification(reference, ussdServiceActivationNumber,
                               criteria);
}

编译器抱怨说,“未报告的异常 PolicyException;必须被捕获或声明为抛出。我不知道为什么它建议我可以抛出,但“抛出”就在方法签名中。

startUSSDNotification 定义为:

public void startUSSDNotification(
    @WebParam(name = "reference", targetNamespace = "http://www.csapi.org/schema/osg/ussd/notification_manager/v1_0/local")
    SimpleReference reference,
    @WebParam(name = "ussdServiceActivationNumber", targetNamespace = "http://www.csapi.org/schema/osg/ussd/notification_manager/v1_0/local")
    String ussdServiceActivationNumber,
    @WebParam(name = "criteria", targetNamespace = "http://www.csapi.org/schema/osg/ussd/notification_manager/v1_0/local")
    String criteria)
    throws PolicyException, ServiceException
;

当我尝试将调用包装在 try...catch 中时,错误仍然存​​在,它会引发另一个错误,指出异常从未在 try 的主体中抛出。我该如何补救?我正在使用 javase7 和 netneans 8

【问题讨论】:

  • 可能是你在方法级别抛出的异常包与预期的包不同。广泛关注 PolicyException 类的包名!!
  • 是的,我从错误的包中导入。现在修好了,谢谢。

标签: java netbeans exception-handling


【解决方案1】:

由于您没有向我们展示实际的编译错误等,我无法确定,但我怀疑有多个名为 PolicyException 的类,并且您声明了错误的类:

  • 检查编译错误的准确措辞

  • 检查您的 import 声明。

【讨论】:

  • 感谢@Stephen C,我导入了错误的“PolicyException”。永远不可能从那个编译器消息中弄清楚这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多