我试着弄清楚这一段时间,所以我有一个方法调用一个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();
我不知道我做错了什么,谢谢你的帮助
答案 0 :(得分:1)
你不能捕获你的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
}