简体   繁体   English

Java未捕获异常

[英]Java Not Catching Exception

Unfortunately, I don't have control over getUserByUserId(String) . 不幸的是,我无法控制getUserByUserId(String) The way it behaves is to return a User if a user if found and to throw a OntNoObjectExistsException if no user is found. 它的行为方式是:如果找到用户,则返回User;如果未找到用户,则抛出OntNoObjectExistsException My problem is that for some reason, the catch doesn't catch OntNoObjectExistsException when it gets thrown. 我的问题是,由于某种原因,该捕获在抛出时不会捕获OntNoObjectExistsException

The type hierarchy for this exception is: OntNoObjectExistsException -> OntException -> Exception -> Throwable . 此异常的类型层次结构为: 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;
}

I tried this code to test the waters and the problem persisted. 我尝试使用此代码来测试水域,问题仍然存在。 Note, I'm catching Throwable . 注意,我正在捕获Throwable

public boolean isUserIdAvailable(String userId) {
    try {
        return super.getUserByUserId(userId) == null;
    } catch (Throwable ex) {
        appLog.error(ex.getMessage());
    }
    return false;
}

Here's the stacktrace: 这是堆栈跟踪:

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)

Also note, this is java 1.4. 另请注意,这是Java 1.4。 Something else I can't control right now. 我现在无法控制的其他事情。

Let me propose a hypothesis. 让我提出一个假设。 WebUser.getUserByUserId contains this code: WebUser.getUserByUserId包含以下代码:

if (userNotFoundCondition) {
  OntNoObjectExistsException e = new OntNoObjectExistsException("User not found");
  logger.error("User not found", e);
  throw e;
}

This hypothesis is 100% consistent with all the evidence you submitted. 该假设与您提交的所有证据100%一致。 In order to move forward with your investigation, you must first disprove this hypothesis. 为了继续进行调查,您必须首先证明这一假设。

您在抛出新的Throwable的超类中捕获异常。

I agree with the the other answer that it is very bad practice to use exceptions for flow control but to actually answer your question have you tried to catch Throwable instead of Exception? 我同意另一个答案,即使用异常进行流控制是非常不好的做法,但实际上要回答您的问题,您是否尝试捕获Throwable而不是Exception?

catch (Throwable t) {
    // handle here.
}

You have no control over getUserByUserId() ; 您无法控制getUserByUserId() however, it seems to be in the same package com.bcbst.dsmart.api , so this answer assumes (so as to move on) it is outside your responsibility within the same project, but you have its source files . 但是,它似乎在com.bcbst.dsmart.api包中,因此, 此答案假定 (继续进行)在同一项目中您不负责, 但您有其源文件

Could there be a mismatch between the sources of the class getUserByUserId() belongs to, and the compiled version that's being used at runtime? getUserByUserId()类所属的源与运行时使用的已编译版本之间是否可能不匹配?

If the throws statements have been modified within that class after they have been compiled, or the exceptions themselves have been changed, this could explain this apparently absurd situation of yours. 如果throws语句在编译后已在该类中进行了修改,或者异常本身已被更改,则这可以解释您的这种显然荒谬的情况。

See this answer on SO for more on that hypothesis. 有关假设的更多信息,请参见SO上的答案

=> Recompile everything, and redeploy. =>重新编译所有内容,然后重新部署。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM