javaloggingexceptionstack-tracejava1.4

The catch doesn't work on an exception when it gets thrown


Unfortunately, I don't have to control over getUserByUserId(String). The way it behaves is to return a User if a user is found and to throw an OntNoObjectExistsException if no user is found. My problem is that for some reason, the catch doesn't work OntNoObjectExistsException when it gets thrown.

The type hierarchy for this exception is: 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.

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. Something else I can't control right now.


Solution

  • Let me propose a hypothesis. WebUser.getUserByUserId contains this code:

    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. In order to move forward with your investigation, you must first disprove this hypothesis.