Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception Handling Question

I have a question regarding exception handling. Consider following Java code snippet.

        try{             //code         }catch(SubSubException subsubex){             //code         }catch(SubException subex){             //code         }catch(Exception ex){             //code         } 

I know this is the recommended way of handling exceptions. But I can achieve the same thing by using following code snippet.

        try{             //code         }catch ( Exception ex){             if( ex instanceof SubException){                               //code             }else if(ex instanceof SubSubException){                 //code             }else{                 //code             }         } 

Can somebody tell me disadvantages of second approach?

like image 527
Upul Bandara Avatar asked Feb 22 '10 05:02

Upul Bandara


People also ask

What is example of exception handling?

Examples include a user providing abnormal input, a file system error being encountered when trying to read or write a file, or a program attempting to divide by zero. Exception handling attempts to gracefully handle these situations so that a program (or worse, an entire system) does not crash.

What is the main reasons for exception handling?

The core advantage of exception handling is to maintain the normal flow of the application. An exception normally disrupts the normal flow of the application; that is why we need to handle exceptions.

What are the 3 types of exceptions?

There are three types of exception—the checked exception, the error and the runtime exception.

What are 3 basic keywords of exception handling mechanism?

Exception handling in C++ consists of three keywords: try, throw and catch: The try statement allows you to define a block of code to be tested for errors while it is being executed. The throw keyword throws an exception when a problem is detected, which lets us create a custom error.


2 Answers

The second approach is less readable. In addition Pokemon exception handling is never the way to go even though your "clever" trick is to use the instanceof keyword. I am not poking fun or mocking you in anyway, but it is best to write code for humans to read and maintain, not for the computer.

like image 155
Woot4Moo Avatar answered Oct 05 '22 15:10

Woot4Moo


Yes, MadMurf points out the most important difference: reachability checking at compile-time. The standard idiom would catch something like this and rightfully prevent it from compiling:

    try {     } catch (IndexOutOfBoundsException iooe) {     } catch (ArrayIndexOutOfBoundsException aiooe) {     } 

The if/instanceof analog proposed in the original question would compile (which is NOT what you'd want because it's erroneous).

The reason why the standard idiom catches the error at compile time is given in JLS 14.21 Unreachable Statements.

  • A catch block C is reachable iff both of the following are true:
    • [...]
    • There is no earlier catch block A in the try statement such that the type of C's parameter is the same as or a subclass of the type of A's parameter.

To further illustrate the point, the following compiles:

    try {     } catch (Exception e) {         if (e instanceof Exception) {         } else if (e instanceof Exception) {         }     } 

As you can see, this "pokemon catching" idiom is much harder to maintain because it circumvents some of the compile-time reachability check enforced in the standard idiom.

To make the point even more clear, whether you did it on purpose or not, you actually rearranged the order in which you checked the exceptions in your original question, a fact that could have easily been missed by others. If SubSubException is a subclass of SubException, the second if condition will NEVER be evaluated, and its body is effectively unreachable code.

The if/instanceof approach is VERY prone to mistakes.

like image 24
polygenelubricants Avatar answered Oct 05 '22 15:10

polygenelubricants