I have the following code Snippet.
class Program
{
static void Main(string[] args)
{
try
{
Thread m_thread = new Thread(() =>
{
//try
//{
checkexc();
//}
//catch (Exception ex)
//{
//}
}
);
m_thread.Start();
}
catch (Exception ex)
{
}
}
static void checkexc()
{
throw new NullReferenceException();
}
}
NullReferenceException is not handled by the covering Try-Catch block. However if i wrap the delegate inside thread() constructor, then it is handled by that Try-Catch. Why doesnot outer Try-Catch handle this exception.
Imagine you have a main road (A), and another road branches off from that road (B).
When a truck goes along A, if it crashes, then A will know about it, and traffic will stop.
When a truck goes along B, if it crashes, then B will know about it, and traffic will stop.
But by what mechanism will B tell A that a truck has crashed on it?
Once the truck is on B, it is not affecting A, unless there is another entry point along A into B.
How will the exception in your other thread be communicated to the main thread? Once the other thread is running, it no longer (directly) communicates with the main thread.