A common usage of catch and finally together is to obtain and use resources in the try block
Deal with exceptional events in catch block
Release the resources in finally block
using System;
public class EHClass
{
public static void Main ()
{
try
{
Console.WriteLine("Executing the try statement.");
throw new NullReferenceException();
}
catch(NullReferenceException e)
{
Console.WriteLine("{0} Caught exception #1.", e);
}
catch
{
Console.WriteLine("Caught exception #2.");
}
finally
{
Console.WriteLine("Executing finally block.");
}
}
}
Output:
Executing the try statement. System.NullReferenceException: Attempted to dereference a null object reference. at EHClass.Main() Caught exception #1. Executing finally block.
No comments:
Post a Comment