Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch separate exceptions or use instanceof - Java 6

Assume that this piece of code is in 20 places and always the same

try {
    // do something
} catch (FirstException e) {
    // log it
} catch (SecondException e) {
    // log it
}

Wouldn't be better to use something like this or instanceof is not good solution?

try {
    // do something
} catch(Exception e) {
    logException(e);
}

void logException(Exception e) {
    if (e instanceof FirstException) {
        // log it
    } else if (e instanceof SecondException) {
        // log it differently
    } else {
        // do something with other exception 
    }
}

The only thing that I really hate about the solution is catching Exception which is definitelly not the best way... Is there any better way?

like image 310
user219882 Avatar asked Mar 21 '13 08:03

user219882


People also ask

Can I catch multiple exceptions Java?

Java allows you to catch multiple type exceptions in a single catch block. It was introduced in Java 7 and helps to optimize code. You can use vertical bar (|) to separate multiple exceptions in catch block.

How do you handle multiple exceptions in catch block in Java?

If a catch block handles multiple exceptions, you can separate them using a pipe (|) and in this case, exception parameter (ex) is final, so you can't change it. The byte code generated by this feature is smaller and reduce code redundancy.

Is try catch faster than if?

If you've one if/else block instead of one try/catch block, and if an exceptions throws in the try/catch block, then the if/else block is faster (if/else block: around 0.0012 milliseconds, try/catch block: around 0.6664 milliseconds). If no exception is thrown with a try/catch block, then a try/catch block is faster.

Can we handle multiple exceptions in single catch block?

Handling More Than One Type of Exception In Java SE 7 and later, a single catch block can handle more than one type of exception. This feature can reduce code duplication and lessen the temptation to catch an overly broad exception.


2 Answers

  1. In Java 7, use catch (FirstException1 | SecondException | ...)
  2. There may be nothing wrong with catch (Exception e)—you do want to log all exceptions, don't you? I would in fact advise catch (Throwable t) because OutOfMemoryError s and StackOverflowErrors also want to be logged.

An advice from many years of experience with logging exceptions is to log them all the same way. The exception message is enough as human-readable text, and what the developer really needs for debugging is the stack trace.

Just be careful about one thing: never catch exceptions too early: catch them at a single place for the whole application, the so-called exception barrier—it is at the level where you enter and exit a unit of work.

If checked exceptions are giving you trouble at the lower level, wrap them into RuntimeException:

try {
  ...
} 
catch (RuntimeException e) {throw e;} 
catch (Exception e) {throw new RuntimeException(e);}

Only if you know precisely and in advance that there is an exception which has business-level meaning to your application, and will not abort the current unit of work, but redirect its flow, is it appropriate to catch that exception at a lower level. In practice such exceptions are rare compared to the totality of all possible exceptions thrown by the application code.

like image 124
Marko Topolnik Avatar answered Sep 24 '22 20:09

Marko Topolnik


The first approach is definitely better. Generally it is a bad practice to catch Exception because in this case you catch RuntimeExceptions too.

like image 38
AlexR Avatar answered Sep 22 '22 20:09

AlexR