O'Reilly logo

C# Primer Plus by Klaus Michelsen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The finally Block

Consider a method that opens a file in its first statements and closes the file just before the method exits. If the flow of execution due to a thrown exception (that substitutes normal execution flow with an abrupt non-sequential path as shown previously) never reaches the end of the method, the file is left unclosed, which can create serious problems in a program. Consequently, we must ensure that, regardless of whether an exception is thrown or not, the file is closed. This can be achieved by inserting the file closing code inside a finally block, which is always executed regardless of the circumstances. A finally block is executed just after the try-catch blocks have completed (regardless of whether an exception is thrown ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required