0

I have a Java Class which does JNDI lookup and returns lookup ejb object. And if there is failure in lookup various exceptions are handled, when there is failure it will return null.

Now when that API is called and lookup fails we will get nullpointer exception. We can do a null check in calling class but I need exact reason for failure .. How to catch those exceptions thrown in base class?

1
  • why catching the lookup exception if you want to handle it somewhere else? Commented Oct 21, 2013 at 7:05

2 Answers 2

1

Don't catch exceptions or if you need to catch them to do some specific work, rethrow them when done so the caller can see there was an exception and handle it itself too.

Like:

method1() {
    try {
        // SomeException is thrown here
    } catch (SomeException e) {
        // do some work because of the exception
        throw e // re-throw or throw new MyException(e)
    }
}

method2() {
    try {
        method1();
    } catch (SomeException e) {
        // something went bad! 
        // do some specific work?
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

But After catching those exception some more statements are there to execute. And if I try to add throw statements I will get "unreachable code Error" for remaining line of codes!!
Class1:- public homeobj m1(){ try{ homeobj=JNDI lookup(); return homeobj; } catch(Exception type1 e1){ } catch(Exception type2 e2){ } //Some more statemnts to execute return null; } Class2:- method m2(){ Class1.m1().somemethod(); // Here we ll get Nullpointer Exception } } This is code structure. If I handle Null here, I will loose exceptions and If I rethrow exception in Class1, remaining code after rethrow will be unreachable
1

If you want to transfer error information upwards in the call chain, the best way to do that is with an exception.

Instead of returning null on a failed look-up you should either let the look-up exception propagate up or wrap it in a custom exception if you want to add additional information.

6 Comments

Class1:- public homeobj m1(){ try{ homeobj=JNDI lookup(); return homeobj; } catch(Exception type1 e1){ } catch(Exception type2 e2){ } //Some more statemnts to execute return null; } Class2:- method m2(){ Class1.m1().somemethod(); // Here we ll get Nullpointer Exception } } This is code structure. If I handle Null here, I will loose exceptions and If I rethrow exception in Class1, remaining code after rethrow will be unreachable
If there are thing you want to do even if the lookup fails, then either do it before the lookup or in the exception handler, before rethrowing the exception.
@user2902067 How about a finally-block?
@ Klas:- I can try that option.. But I dont have rights to change that class :(
@Viktor :- In finally block what I can do for above scenario
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.