No return statement in the finally clause, please.

You cannot put return statement in the finally clause because Control cannot leave the body of a finally clause (Compiler error code CS0157)…Why? MSDN says that all statement in the finally clause must execute. MADN also states that

The purpose of a finally statement is to ensure that the necessary cleanup of objects, usually objects that are holding external resources, happens immediately, even if an exception is thrown.

Thus for releasing all locks and hold objects or in other words control cannot leave the finally block before finishing the cleanup task. However, an exception can be occurred in the finally block. And if it is not handled properly, code execution will stop and error is thrown.

Technorati Tags: ,,
Digg This
Advertisements

One thought on “No return statement in the finally clause, please.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s