Exception in a Thread other than Main thread

  • Thread starter Mahesh Devjibhai Dhola
  • Start date
M

Mahesh Devjibhai Dhola

Hi,
I have one program, where i am using IO and Socket Asynchronous methods
"BeginXXX" and "EndXXX".

Many time, it happens that in Async delegate method, some exception occurs
and if i dont handle it, it shows standard Unhandled exception box even I
have handled the exception using try/catch in the main thread which is
caller of that method.
That means, the exception cant be handled by catch block written in main
thread if the exception occurs in a thread other than main.

My intention is to get the exception message so i can handle it gracefully
and show a userfriendly message on that exception. So usual, i use re-throw
the exceptions and handle in main UI blocks, if its not possible to return
values or if return value is possible then as return value so in UI blocks i
can show user friendly messages.

But as mentioned above, in multi-threaded program, if such condition happens
then what is the solution?
Is it possible to do that anyhow?
Is it by design or my program has bad-design?
If this is bad design then please suggest better way,

Thanking you in Advance,
Regards,
Mahesh
 
T

Tasos Vogiatzoglou

Use

Application.ThreadException+=new
System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

Tasos.
 
M

Mahesh Devjibhai Dhola

Hi,
Thanks for the kool pointer.
But the problem is we have many of independent components and some of
components internally uses other components and now inner most component
class has thrown some exception in different thread [other than main thread]
then will it be caught in Main thread in Main application?
e.g.
Main App [with UI] -> Comp1 -> Comp2 -> Comp3
Now if Comp3 throws an exception in diff thread then ThreadException will
catch that exception but what if i want to catch it in Comp1 or Comp2 where
i havnt not used UI [means Windows Forms] ?
This ThreadException event force me to use Windows.Forms assemly so is there
any other similar way?
Also, this is a single event method where i am getting this exception but if
i want to catch exception by caller and it may be any seperate methods so is
it possible to catch this exception in caller method somehow? because its
tough to handle all the exception in one method and who throws that
exception, how we can know if its really hugh application with lots of
components?

Else this will be definately helpful to me.

Thanks,
Mahesh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top