Information Technology Reference
In-Depth Information
Example of Searching Down the Call Stack
In this program, Main starts execution and calls method A , which calls method B . A description
and diagram of the process are given in the following code and in Figure 11-9.
class Program
{
static void Main()
{
MyClass MCls = new MyClass();
try
{ MCls.A(); }
catch (DivideByZeroException e)
{ Console.WriteLine("catch clause in Main()"); }
finally
{ Console.WriteLine("finally clause in Main()"); }
Console.WriteLine("After try statement in Main.");
Console.WriteLine(" -- Keep running.");
}
}
class MyClass
{
public void A()
{
try
{ B(); }
catch (System.NullReferenceException)
{ Console.WriteLine("catch clause in A()"); }
finally
{ Console.WriteLine("finally clause in A()"); }
}
void B()
{
int x = 10, y = 0;
try
{ x /= y; }
catch (System.IndexOutOfRangeException)
{ Console.WriteLine("catch clause in B()"); }
finally
{ Console.WriteLine("finally clause in B()"); }
}
}
Search WWH ::




Custom Search