Nesting try
/catch
or @try
/@catch
blocks severely impacts the readability of source code because
it makes it too difficult to understand which block will catch which exception.
This C++ example also applies to Objective-C.
Noncompliant code example
try {
try { // Noncompliant
doSomething();
} catch (RuntimeException e) {
/* Ignore */
}
doSomethingElse();
} catch (Exception e) {
/* ... */
}
Compliant solution
try {
dedicatedMethod(); // Compliant
doSomethingElse();
} catch (Exception e) {
/* ... */
}
/* ... */
private void dedicatedMethod() {
try { // Compliant
doSomething();
} catch (RuntimeException e) {
/* Ignore */
}
}