Thursday, September 3, 2015

Compile Error CS0157: Control cannot leave the body of a finally clause

Few days back, I was working on an exception. So I was using "try-catch-finally" in a method, which is a very common technique for handling exception. For curiosity, I returned a value from the finally block and guess what?? I got this compiler error...

In the exception handling statements finally block do those things (such as closing database connection or file handler) which always have to be done whether  exception occurred or not. Actually this block is used to release the resources. So it can not allow any jump statement to skip it's block. finally always push the program for success whatever happens. It never misses a single thing. Even if you use jump statements in try or catch this block will always be executed.

So this error happens when any jump statement (break, continue, goto and return) is used in the finally block of exception handling statements (try-throw-catch-finally).

Image 1: Compile Error CS0157 in Xamarin Studio


 In image 1 a jump statement (return) is used in finally block. 






Image 2: Successfully running the program.





In Image 2 the jump statement is removed from finally block.












Enjoy your coding. Love "bugs" and "errors"

No comments:

Post a Comment