1

Please pay attention: caller throws parentexception only!!

Say that aexception, and bexception inherit from parentexception.

In method af, it throws aexception, bexception and parentexception.

void af() throws aexception, bexception, parentexception {}

The method caller calls af and throw parentexception only.

void caller() throws parentexception

Here we lost the information of subclasses of parentexception.

The method rootCaller calls the method caller and rootcaller can only catch parentexception thrown by caller and using the following exception process catch block:

void rootCaller() {
    try {
        caller(); 
    } catch(parentexception e) {
    if(e instanceof aexception) {
        ......
    } else   if(e instanceof bexception) {
        ......
    } else   if(e instanceof parentexception) {
        ......
    } else {
    ......
    }
}

This is so ugly and very easy to forget some subclass of parentexception if the subclasses are too many.

Is there anyway to improve such code?

Current answer can not give me any idea:

1, rootCaller cannnot use multi-catch to simplify the process cause caller only throw parentexception.

2, because caller only throw parentexception, there is not any other exception check if the af is changed latter to throws more than aexception and bexception, say cexception.

  • There's code formatting. Indent, or use front ticks,```` for inline code – keyser May 29 '14 at 19:45
  • 3
    Solution: **don't catch `Exception`**. Catch individual exceptions. That's why you have the `catch` clauses in the first place! – awksp May 29 '14 at 19:45
  • 9
    Why don't you use multiple `catch` clauses? – Jeroen Vannevel May 29 '14 at 19:45
  • Is there any specific reason of checking the instance of the exception? – Braj May 29 '14 at 19:48
  • Do you need to handle each exception caught differently? If not, just have a single catch for parentexception. – JamesB May 29 '14 at 19:53
  • As the code show. From function `rootCaller`, it cannot catch the subclass of parentException. because `caller` `only` throw the parentException. that's why I cannot use multiple catch clauses. – user1542687 May 29 '14 at 19:57

1 Answers1

7

As others have suggested in the comments, you should be using multiple catch clauses.

void rootCaller() {
    try {
        caller();
    } catch (AException e) {
        // ...
    } catch (ParentException e) {
        // ...
    } catch (BException e) {
        // ...
    } catch (AnotherException e) {
        // ...
    } catch (Exception e) {
        // ...
    }
}

The order of the catches matters too. The Exception will be tested against each case in turn and only trigger the first one that matches.

So for example with AException and BException extending ParentException in my above code the catch (BException e) block can never be reached as catch (ParentException e) is reached and executed first.

indivisible
  • 4,892
  • 4
  • 31
  • 50
  • 1, after catch (ParentException e), the BException would be cared anymore. and 2, as my code show, the rootCaller cannot catch the subclass of the ParentException because it don't know them from caller function – user1542687 May 29 '14 at 20:04
  • @user1542687 Read my final paragraph. I did that on purpose to illustrate that the order does matter. – indivisible May 29 '14 at 20:04