Back to Intellij Community

TooBroadCatch

java/java-impl/resources/inspectionDescriptions/TooBroadCatch.html

2025.3-rc-2933 B
Original Source

Reports catch blocks with parameters that are more generic than the exception thrown by the corresponding try block.

Example:

try {
    File file = new File(pathToFile);
    return file.getAbsolutePath();
  } catch (Exception ex) { // warning: 'catch' of 'Exception' is too broad, masking exceptions 'RuntimeException'
    return defaultFilePath;
  }

After the quick-fix is applied:

try {
    File file = new File(pathToFile);
    return file.getAbsolutePath();
  } catch (RuntimeException ex) {
    return defaultFilePath;
  }

Configure the inspection:

  • Use the Only warn on RuntimeException, Exception, Error or Throwable option to have this inspection warn only on the most generic exceptions.
  • Use the Ignore exceptions which hide others but are themselves thrown option to ignore any exceptions that hide other exceptions but still may be thrown and thus are technically not overly broad.