Avoid nested try-catch blocks

Why you should care

Although this is sometimes unavailable, nesting try/catch blocks severely impacts the readability of the source code as it makes it difficult to understand which block will catch which exception.

How we detect

CAST Highlight counts one occurrence each time a try block contains one or more nested other try.

Bad Code 

try {
  // some stuff
  // ....
  if (toto) {
    try {         // ===> VIOLATION
      // another stuff
      // ...
    }
    catch (Exception e) {
      throw e;
    }
  }
}
catch (Exception e) {
  throw e;
}

Good Code 

try {
  // some stuff
  // ....
}
catch (Exception e) {
 throw e;
}
if (toto) {
  try {
    // another stuff
    // ...
  }
  catch (Exception e) {
     throw e;
  }
}

References

5362

About CAST and Highlight’s Code Insights

Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.