Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multithreading, if a thread get crashed what will happen to application

I am new to multithreaded application. I have few doubts before starting working on it. Can anyone clear these doubts?

  1. How to handle exceptions in multithreaded application?
  2. If there is any exceptions in any 1 thread, will the whole application will come down? or all other threads will continue?
like image 336
TAdhav Avatar asked Dec 16 '25 22:12

TAdhav


1 Answers

How to handle exceptions in multithreaded application?

The best way is inside the thread, but sometimes eg. (thread pool) this is hard. To handle unhandled exceptions depends on the type of application:

  • WPF: Use Application.DispatcherUnhandledException
  • Console or Service: Use AppDomain.UnhandledException

ASP.NET, WinForms, ... have their own mechanisms.

But consider: especially while developing to allow the default handling and falling into the debugger (adding if (Debugger.IsAttached) { Debugger.Break(); } can be very helpful to define a permanant breakpoint

If there is any exceptions in any 1 thread, will the whole application will come down?

It depends. In ASP.NET: No (if debugging is enabled you'll see a Yellow Screen of Death, otherwise a 500 server error result); in other hosts: depends (but generally the answer is Yes the process will terminate).

Some of the details of this have changed between .NET versions – typically getting stricter, so you need to do your research. Finally: in .NET 4 certainly, possibly before as well, certain exceptions (eg. StackOverflowException) cannot be caught because there is no reliable way to have consistent process state if they are thrown.

like image 137
Richard Avatar answered Dec 19 '25 15:12

Richard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!