0

Think about two cases case1 and case2 plus two methods method1 and method2. Say method1 solves case1 and method2 solves case2. Now, I have a program that might end up with case1 or case2. In my codes, I call method1 no matter what case happens. But, if case2 occurs, method1 gives a nullpointerexception.

What I want is the following: my codes should call method1 first, if an exception occurs, then method2 is called. How am I gonna do that? Since I have no info about try and catch, I really need some help!

4
  • 2
    I think some code is better than any explanations. Commented May 26, 2011 at 18:17
  • I guess you are right. Say I have TreeNode<City> parentNode which will be the left hand of an equality and the right hand will be search(parent,this) or search2(parent,this). Yet, search and search2 might return null, which I do not want. Now, how should I implement? Commented May 26, 2011 at 18:31
  • please edit your question and post some code sample, not just variable names and method signatures. Commented May 26, 2011 at 18:33
  • You could call search1(), then check whether the result is null, and if so, then call search2. If search2 is also null, the equality test should fail. Commented May 26, 2011 at 18:35

2 Answers 2

3

You could do this:

    try {
        method1();
    }
    catch ( Exception e ) {
        method2();
    } 

That said, it's typically better to rely on exceptions only for exceptional conditions. For normal flow of control, you can use an if:

    if ( isCase2() ) {
        method2();
    }
    else {
        method1();
    }
Sign up to request clarification or add additional context in comments.

3 Comments

As @Vladimir posted, if you're going to go with try/catch, it's better to throw your own exception object from method1 than to rely on a NPE (which might arise even in case 1 due to other problems).
Agreed. The first example above is intended to satisfy the OP literally: "my codes should call method1 first, if an exception occurs, then method2 is called." If you can distinguish between case1 and case2 a priori, then that's preferable to using any exception.
Absolutely. Use exceptions for exceptional situations. This sounds like having one case versus another is not exceptional at all.
2

Catching NullPointerException is a bad pratice - you may catch not the particular exception you want to catch. You have two options:

1) Throw your own exception and catch it later:

  public void method1(Case caze) throws MyException {
    if (case.getType() == CaseType.CaseOne) {
       // processing
    } else {
       throw new MyException("Wrong case type");
    }
  }

And the client code:

try {
   method1(caze);
} catch (MyException e) {
   // log the excpetion
   method2(caze);
}

2) Return a boolean flag, indicating that the processing has been succesfully finished.

Remember, that it is alway better to analyze the values than use try-catch mechanism in your situations. I would suggest variant #2 for you.

Comments

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.