[英]Java Not Catching Exception
不幸的是,我無法控制getUserByUserId(String)
。 它的行為方式是:如果找到用戶,則返回User;如果未找到用戶,則拋出OntNoObjectExistsException
。 我的問題是,由於某種原因,該捕獲在拋出時不會捕獲OntNoObjectExistsException
。
此異常的類型層次結構為: OntNoObjectExistsException
> OntException
> Exception
-> Throwable
。
public boolean isUserIdAvailable(String userId) {
try {
return super.getUserByUserId(userId) == null;
} catch (OntNoObjectExistsException e){
return true;
} catch (Exception ex) {
appLog.error(ex.getMessage());
}
return false;
}
我嘗試使用此代碼來測試水域,問題仍然存在。 注意,我正在捕獲Throwable
。
public boolean isUserIdAvailable(String userId) {
try {
return super.getUserByUserId(userId) == null;
} catch (Throwable ex) {
appLog.error(ex.getMessage());
}
return false;
}
這是堆棧跟蹤:
com.opennetwork.exception.OntNoObjectExistsException: User not found
at com.bcbst.dsmart.api.WebUser.getUserByUserId(WebUser.java:411)
at com.bcbst.dsmart.api.WebProspectiveMemberBean.isUserIdAvailable(WebProspectiveMemberBean.java:71)
at com.bcbst.dsmart.api.EJSLocalStatelessWebProspectiveMember_ce00ef7b.isUserIdAvailable(EJSLocalStatelessWebProspectiveMember_ce00ef7b.java:120)
at com.bcbst.prospectivememberweb.actions.UsageagreementAction.execute(UsageagreementAction.java:61)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
另請注意,這是Java 1.4。 我現在無法控制的其他事情。
讓我提出一個假設。 WebUser.getUserByUserId
包含以下代碼:
if (userNotFoundCondition) {
OntNoObjectExistsException e = new OntNoObjectExistsException("User not found");
logger.error("User not found", e);
throw e;
}
該假設與您提交的所有證據100%一致。 為了繼續進行調查,您必須首先證明這一假設。
您在拋出新的Throwable
的超類中捕獲異常。
我同意另一個答案,即使用異常進行流控制是非常不好的做法,但實際上要回答您的問題,您是否嘗試捕獲Throwable而不是Exception?
catch (Throwable t) {
// handle here.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.