The following Code will demonstrate the working of FirstChanceExceptionEventArgs Class.
class Worker : MarshalByRefObject
{
public void raiseException()
{
try
{
throw new Exception("I am Exception");
}
catch { }
}
}
class Program
{
static void Main(string[] args)
{
AppDomain.CurrentDomain.FirstChanceException += new EventHandler<System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs>(CurrentDomain_FirstChanceException);
AppDomain newDomain = AppDomain.CreateDomain("AD01");
newDomain.FirstChanceException += new EventHandler<System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs>(newDomain_FirstChanceException);
Assembly assem = Assembly.GetExecutingAssembly();
Worker worker = (Worker)newDomain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, "TuplePractice.Worker");
worker.raiseException();
int k = 0;
int i = 1;
try
{
i = 1 / k;
}
catch
{
Console.WriteLine("First Exception occurs");
}
try
{
i = 1 / k;
}
catch
{
Console.WriteLine("Second Exception occurs");
}
return;
}
static void
newDomain_FirstChanceException(object sender,
FirstChanceExceptionEventArgs
e)
{
Console.WriteLine("FirstChanceException
event raised in {0}: {1}", AppDomain.CurrentDomain.FriendlyName,
e.Exception.Message);
}
static void
CurrentDomain_FirstChanceException(object
sender, FirstChanceExceptionEventArgs
e)
{
Console.WriteLine("FirstChanceException
event raised in {0}: {1}", AppDomain.CurrentDomain.FriendlyName,
e.Exception.Message);
}
}
Output
FirstChanceException event raised in AD01: I am Exception
FirstChanceException event raised in TuplePractice.exe:
Attempted to divide by zero.
First Exception occurs
FirstChanceException event raised in TuplePractice.exe:
Attempted to divide by zero.
Second Exception occurs
Press any key to continue . . .
RemarksOutput of the porgram shows that all registerd event handlers of AppDomain.CurrentDomain.FirstChanceException are triggered prior to the catch block of respectice try block.
Further details can be found here.
No comments:
Post a Comment