Nesting try-catch blocks severely impacts the readability of source code because it makes it to difficult to understand which block will catch which exception.
The following code:
try {
try { // Non-Compliant
doSomething();
} catch (RuntimeException e) {
/* Ignore */
}
doSomethingElse();
} catch (Exception e) {
/* ... */
}
should be refactored into:
try {
dedicatedMethod(); // Compliant
doSomethingElse();
} catch (Exception e) {
/* ... */
}
/* ... */
private void dedicatedMethod() {
try { // Compliant
doSomething();
} catch (RuntimeException e) {
/* Ignore */
}
}