It depends on what the thread is doing and the context of the thread in your application. Generally speaking, you should follow the golden rule of thumb: do not catch an exception unless you know how to handle it (I am simplifying of course, but it's a rule of thumb).
It depends on what the thread is doing and the context of the thread in your application. Generally speaking, you should follow the golden rule of thumb: do not catch an exception unless you know how to handle it. (I am simplifying of course, but it's a rule of thumb).
Since we are talking about System. Exception and not some subclass, I assume that you would not in fact know how to handle the exception. Logging the error and letting the application terminate (the only legitimate reason to catch an exception you can't handle) can be done without catching the exception from within the thread that raised it, so the short answer is no, it's no OK.
If I remember correctly . NET 1 actually caught and swallowed all exceptions raised on background threads. This led to so many problems in badly written programs that MS changed the behavior in .
NET 2 to let the exceptions crash the app -- and you can imagine they had very good reason to make such a breaking change. Update regarding BackgroundWorker: Please do not mistake the usage model of BackgroundWorker for "swallowing System. Exception is OK".
Here's what the docs for BackgroundWorker. RunWorkerCompleted say: Your RunWorkerCompleted event handler should always check the AsyncCompletedEventArgs. Error and AsyncCompletedEventArgs.
Cancelled properties before accessing the RunWorkerCompletedEventArgs. Result property. If an exception was raised or if the operation was canceled, accessing the RunWorkerCompletedEventArgs.
Result property raises an exception. Choosing to disregard this suggestion or ignoring the return value on purpose (otherwise an exception will still be thrown! ) is, simply put, bad programming practice.
Agree. Catching just for the sake of not letting the application crash causes that it seems that nothing happens, but in fact there was an important failure. At least show an error message, log all the information possible anc shutdown the application.
Otherwise you are just hiding an error. – Haplo Apr 4 at 12:23 @Haplo - Jon says it's not OK and you agree but then you suggest showing an error message, logging and shutting down, which requires that it be done. ...? – Kieren Johnstone Apr 4 at 12:28 @Kieren: No, none of that requires that you catch the exception.
Jon suggests handling the AppDomain. UnhandledException event, which is the correct way to do this. I can only assume that's what @Haplo is referring to.
– Cody Gray Apr 4 at 12:30 1 @Joe: Please read the update and tell me if you still think the designers of BackgroundWorker disagree. – Jon Apr 4 at 12:49 1 @Joe: Also, the session being maintained etc is totally irrelevant. If Word crashes, there's code in it that allows you to continue working without losing your unsaved changes.
Maintaining state and crashing are orthogonal. – Jon Apr 4 at 14:08.
Yes - it's an excellent idea :) Umm, I can't see why people are suggesting it's not a good idea to catch top-level exceptions. Seriously, you don't catch them? From an actual day-to-day development standpoint, if your background thread has a top-level exception, you want to know about it.
You don't want the app to crash, the default behaviour - users don't like that, surprisingly. This is one of the few places you definitely want to catch exceptions and log/recover.
Unfortunately, that's not entirely clear. Either way, I suggest editing your answer to provide more information. – Cody Gray Apr 4 at 12:16 I can't think what more information is needed.
Is it a good idea? Yes.Is it OK? Yes... – Kieren Johnstone Apr 4 at 12:22 Hrm, I interpret questions like this as asking why (or why not).
Strictly yes/no answers aren't very useful because they don't help anyone to understand things. Why is it an excellent idea? – Cody Gray Apr 4 at 12:24 I've added some.
I am stunned people are suggesting it's not good practise / not a good idea.Do these people write apps that are used by anyone? – Kieren Johnstone Apr 4 at 12:26 Whoa, hold on. You can't just say "log/recover".
They're totally different things. You should not catch the exception just to log it. You should only catch the exception if you can recover from it.
But that's precisely why you shouldn't catch the base Exception class—by definition, it includes exceptions that you can't possibly recover from. I agree that it might be a good idea to catch specific exceptions derived from the base class, but people are saying it's never a good idea to catch System.Exception. I can't speak for them, but I've certainly written production apps.
– Cody Gray Apr 4 at 12:28.
Officially it's not good practice, but sometimes it is done. The arguments are exactly the same as they are for doing this on a "main" thread. One major problem is that if you swallow errors like this, then your application may function very incorrectly - e.g.It might overwrite crucial user data - rather than terminating.
If you do chose to go this route, you might want to be careful to exclude ThreadAbortException from your catching - this is an "expected exception" if anyone ever aborts a worker thread (which may or may not be the case in your app).
I don't agree; he says the top level of a thread. That means when a thread fails, you can recover or log - it won't 'swallow' exceptions will it? – Kieren Johnstone Apr 4 at 12:26 1 I think the answer is that it depends on what the exception is... how are you deciding whether to recover or log?
If you are proposing there are certain types of errors where you want your application to recover then I'm happy for you to recommend that as "best practice" - but that's not catching System.Exception. If you want to recommend some generic "log and exit" pattern as best practice then I'm not going to argue... However, I'm still going to stick by "officially it's not good practice" - I can't recall anyone like Richter recommending this? – Stuart Apr 4 at 12:59.
Only handle exception you could do something about. To not show yellow screen I personally prefer to catch unhandeld excetpion in the global. Asax event Application_error.
We are generally not meant to create new thread directly. Only in a few circumstances it is acceptable. So if you do use new Thread() especially in UI , that would be frowned upon.It is encouraged to use BackgroundWorker or Task which encapsulates thread exception handling so no exception handling in terms of catch block would be necessary.
These 4 scenarios are acceptable cases where you want your own thread (From CLR via C#): I highly recommend that you use the thread pool to execute asynchronous compute-bound operations whenever possible. However, there are some occasions when you might want to explicitly create a thread dedicated to executing a particular compute-bound operation. Typically, you'd want to create a dedicated thread if you're going to execute code that requires the thread to be in a particular state that is not normal for a thread pool thread.
For example, I'd create a dedicated thread if I wanted the thread to run at a special priority (all thread pool threads run at normal priority, and you should not alter a thread pool thread's priority). I would also consider creating and using my own thread if I wanted to make the thread a foreground thread (all thread pool threads are background threads), thereby preventing the application from dying until my thread has completed its task. I'd also use a dedicated thread if the compute-bound task were extremely long running; this way, I would not be taxing the thread pool's logic as it tries to figure out whether to create an additional thread.
Finally, I'd use a dedicated thread if I wanted to start a thread and possibly abort it prematurely by calling Thread's Abort method (discussed in Chapter 21, "CLR Hosting and AppDomains").
1: This is a going too far. Of course we are meant to use threading directly when it's the most suitable option. – Jon Apr 4 at 12:13 (citation needed) – Cody Gray Apr 4 at 12:14 Please see my updates.
– Aliostad Apr 4 at 12:20 Where does that say "do not use new Thread()"? – Cody Gray Apr 4 at 12:25 I highly recommend that you use the thread pool. So use thread pool (used by Task and BackgroundWorker) instead of creating your own thread.
– Aliostad Apr 4 at 12:27.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.