Inspectopedia Help

'try finally' can be replaced with 'try' with resources

Reports try-finally statements that can use Java 7 Automatic Resource Management, which is less error-prone.

A quick-fix is available to convert a try-finally statement into a try-with-resources statement.

Example:

PrintStream printStream = new PrintStream(fileName); try { printStream.print(true); } finally { printStream.close(); }

A quick-fix is provided to pass the cause to a constructor:

try (PrintStream printStream = new PrintStream(fileName)) { printStream.print(true); }

Locating this inspection

By ID

Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.

TryFinallyCanBeTryWithResources
Via Settings dialog

Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.

Settings or Preferences | Editor | Inspections | Java | Java language level migration aids | Java 7

This inspection depends on the Java feature 'Try-with-resources', which is available since Java 7.

Inspection Details

By default bundled with:

IntelliJ IDEA 2024.2, Qodana for JVM 2024.2,

Can be installed with plugin:

Java, 242.22892

Last modified: 11 September 2024