This page contains temporary result from dicussions on error handling. These guidelines must be reviewed and approved before being promoted to the Error Handling page. |
Huge try-catch block complicates refactoring
try {
is = new FileInputStream(fileName); // throws FileNotFoundException
... // a lot of other stuff, which can't throw FileNotFoundException
... // also you can imagine that one day after refactorings this block will contain call to method, which also may throw FileNotFoundException
} catch (FileNotFoundException e) {
...
} |
Not correct propagation :
try {
// do risky stuff
} catch (Exception e) {
throw new RuntimeException(e); // unnecessary wrapping in case if e instanceof RuntimeException
} |
Better :
try {
doRiskyStuff();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
} |
With Google Guava :
try {
// do risky stuff
} catch (Exception e) {
throw Throwables.propagate(e);
} |