Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a library written to work in multi-threading environment, but the functions may throw exceptions from threads, so now how can i handle such exceptions??

Thanks,
Dan
Posted

Read this[^]. Small, but effective answer on StackOverflow. Hope it helps.
 
Share this answer
 
Comments
Dan Suthar 11-Jun-11 5:47am    
:D, so they say, don't throw it at all, raise an event, but i have a library, modification is not possible ..
Sander Rossel 11-Jun-11 7:54am    
Well, not sure then. Are you sure the library has no way of handling or reporting errors? I have worked with a library that does not allow the catching of Exceptions, but raises Events when Exceptions occur. If your library is a 3rd party tool I guess reading the manual or asking on their website would be your best option. Without knowing more about your specific problem I can't really say much else.
Dan Suthar 11-Jun-11 14:21pm    
Thanks
Sergey Alexandrovich Kryukov 11-Jun-11 21:39pm    
Basically, correct recipe, my 5, but there is a lot more to it.
Please see my answer.
--SA
This is not to hard to understand. Let's think.

Each thread works in its own stack. Exceptions also work on stack: the thrown exception is propagated to the point of the nearest try statement "back in time" on the same thread (thread unwinding). Therefore you need to catch all exceptions on the top of the stack of each thread, to avoid uncontrolled termination of threads. At the same time, it's very important not to overdue catching exceptions; as a matter or rule, an exception should never be caught at the level where there is not specific exception handling for the type of exception being caught. It's equally important not to block propagation of exception. There are relatively rare exclusions from this rule, mostly as a work-around of some "bad" code inaccessible for patching.

Please see more detail in my past answers on the topic:
How do i make a loop that will stop when a scrollbar reaches the bottom[^],
When i run an application an exception is caught how to handle this?[^],
throw . .then ... rethrowing[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900