Home > Code Analysis > Code Issues – Try statement without catch or finally

Code Issues – Try statement without catch or finally

January 24th, 2011

Cause:

This code issue shows an error if a ‘try’ statement doesn’t have a ‘catch’ or ‘finally’ statement. The try block must be completed, whether with catch or finally blocks.

The try block contains the guarded code that may cause the exception. The block is executed until an exception is thrown or it is completed successfully.

The catch block is used to take a control when any or a specific exception has been thrown.

The finally block is useful for cleaning up any resources allocated in the try block, as well as running any code that must execute even if there is an exception. Control is always passed to the finally block regardless of how the try block exits.

Sample:

CodeRush Code Issues - Try statement without catch or finally

How to fix:

  • Add the missing catch block:

CodeRush Try Statement Without Catch Or Finally Fix 1

  • Add the missing finally block:

CodeRush Try Statement Without Catch Or Finally Fix 2

  • Add the missing catch and finally blocks:

CodeRush Try Statement Without Catch Or Finally Fix 3

—–
Products: CodeRush Pro
Versions: 12.1 and up
VS IDEs: any
Updated: Nov/19/2012
ID: C057

Similar Posts:

  1. No comments yet. Be the first and leave a comment!