views:

118

answers:

4

Recently i have attended an interview . A code snippet is given to me.I know,the interviewer took it from albhari's threading sample.

public static void Main() 
{
try 
{
new Thread (Go).Start();
}
catch (Exception ex)
 {
   // We'll never get here!
   Console.WriteLine ("Exception!");
}
static void Go() { throw null; }
}

The modification of the above code as

public static void Main()
 {
  new Thread (Go).Start();
}
static void Go() {
try {
...
throw null; // this exception will get caught below
...
}
catch (Exception ex) {
Typically log the exception, and/or signal another thread
that we've come unstuck
...
}

would be the good candidate to handle the exception.

I have been asked, "Except the above trail what are the other alternatives would fit as good solution?. It was hard to find the alternative,so i raise it here to gather your suggestion.

+6  A: 

Exception thrown in a thread normally couldn't be caught in another thread.

You'd better to catch it in function Go and pass it to main thread explicitly.

However, if you just want to log all unhandled messages from all threads, you may use AppDomain.UnhandledException event or equivalent events at Application class if you are developing WinForms or WPF app.

elder_george
...but be aware that you cannot handle the exception in AppDomain.UnhandledException, you get notified but the application will be shut down anyways.
Lucero
It is possible to prevent application stopping by setting v1.x compatibility mode. For this a <legacyUnhandledExceptionPolicy enabled="1"/> element must be added to app.config in the <runtime section
elder_george
+1  A: 

You can use the AppDomain.UnhandledException event

Thomas Levesque
+3  A: 

what are the other alternatives would fit as good solution?.

Solution to what? What problem are you trying to solve?

If you use BackgroundWorker, as opposed to Thread, it has an RunWorkerCompleted event, and within that you can check the RunWorkerCompletedEventArgs param for the Error property. This generally is used in WinForms or WPF apps, because there is good support for BackgroundWorker in the Visual Studio designer.

You could also define a delegate for Go(), and call BeginInvoke() on it. Of course you need the EndInvoke() too.

Also, it's generally not a good idea to start up random threads. ThreadPool.QueueUserWorkItem, BackgroundWorker, or asynch delegates all use the ThreadPool, and are recommended.

Cheeso
to the same code they asked alternatives.
well what I mean is this: the original code was broken. Your first option is an alternative. IF it is satisfactory, then you need no other alternatives. What problem are you trying to solve? "an alternative to the original code" could be *anything*. You could submit code that solves sudoku puzzles - that would be an alternative. Would it be appropriate? Without requirements, who can say?
Cheeso
No sir,the interviewer asked that question. :) so i did not tell anything.Moreover i did not the know how to tackle it. :) Thanks for your suggestion.
@Cheeso, please read the question correctly, no one is in need of any suggestion, he faced this at an interview where the smart guy tells him to find an alternative, i believe the requester truely knows enough about everything what you are suggesting.
Akash Kava
A: 

Except AppDomain.UnhandledException there is no alternative, however by using any other third party library code or using BackgroundWorker or anything they would internally do exactly same thing what is done on the code above.

If you use Reflector, in class BackgroundWorker it does exactly same thing to catch exception and notify ahead.

Akash Kava
Can the smartest guy puting -1 can explain whats wrong in this answer?
Akash Kava