dharga
dharga

Reputation: 2217

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.

Upvotes: 1

Views: 1002

Answers (4)

Alain BECKER
Alain BECKER

Reputation: 798

You have no control over 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.

Could there be a mismatch between the sources of the class getUserByUserId() belongs to, and the compiled version that's being used at runtime?

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.

See this answer on SO for more on that hypothesis.

=> Recompile everything, and redeploy.

Upvotes: 0

Marko Topolnik
Marko Topolnik

Reputation: 200158

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.

Upvotes: 2

Roman C
Roman C

Reputation: 1

You are catching exception in the superclass where you throw new Throwable.

Upvotes: 1

Ben
Ben

Reputation: 2801

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?

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

Upvotes: 1

Related Questions