Finally block contains exception handler and cleanup code
Finally block executes regardless of how try block exited
Code listing: Try-Finally
This program generates System.InvalidCastException but also prints value of i
using System;
public class TestTryFinally
{
public static void Main()
{
int i = 123;
string s = "Some string";
object o = s;
try
{
// Invalid conversion; o contains a string not an int
i = (int) o;
}
finally
{
Console.Write("i = {0}", i);
}
}
}
No comments:
Post a Comment