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:
How to fix:
- Add the missing catch block:
- Add the missing finally block:
- Add the missing catch and finally blocks: