我试图弄清楚这一点,所以我有一个方法正在调用count()方法,该方法应该抛出异常

count()方法

public int count() throws ParseException {
  return something that may throw the ParseException
}


然后从这里打电话

ParseQuery<ParseObject> query = ParseQuery.getQuery(className);
query.fromLocalDatastore();

int result = 0;

try {
   result = query.count();
} catch (ParseException e) {
   result = 0;
}

return result;


现在,我一直在尝试不同的方案,但是无论IDE是否仍未编译,都会出现以下错误

Error:(254, 11) error: exception ParseException is never thrown in body of corresponding try statement

Error:(253, 33) error: unreported exception ParseException; must be caught or declared to be thrown


在行result = query.count();

我不知道我在做什么错,谢谢您的帮助

最佳答案

您无法捕获try块永远不会抛出的异常,提示错误

try {
   result = query.count(); // this statement not throwing ParseException
} catch (ParseException e) { // you are trying to catch ParseException that never gonna throw.
   result = 0;
}


它像是

    try {
           .... code // throws ExceptionA
        }
    catch (ExceptionB e) { // and calling ExceptionB

        }

08-03 12:44