Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello...What are best practices for using try catch blocks,In my project i am using try catch block for Sql Executions and Calculations parts only..is it ok???
Pls Help
Posted
Comments
Obaid Ahmed 24-Feb-11 1:27am    
Exception Handling has to be present every where in code, where code will possible to crash in any worst situation.
Sergey Alexandrovich Kryukov 24-Feb-11 1:59am    
Not accurate, wrong in many cases. Structural Exception handling is designed to minimize and isolate error processing.
--SA
Obaid Ahmed 24-Feb-11 2:07am    
ya ya.. I'm not talking about Structural Exception Handling, yes you are rite!
Sergey Alexandrovich Kryukov 24-Feb-11 3:25am    
All other error handling techniques are rendered obsolete with inventions of structural exception handling by Barbara Liskov and by implementation of this technology on all platforms.
--SA

Relevant article on MSDN:
http://msdn.microsoft.com/en-us/library/seyhszts.aspx[^]

Popular article on this topic here on CodeProject:
Exception Handling Best Practices in .NET[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Feb-11 1:28am    
Good, my 5.
From all developer's mistakes about exceptions one is a champion in creating disaster; I dedicated my answer to this topic, please see.
--SA
Nuri Ismail 24-Feb-11 3:18am    
Very useful links! 5+
Espen Harlinn 24-Feb-11 4:06am    
Good links :)
Most frequent and disrupting mistake is in the catch block: many developer catch exception and blocking its further propagation up the stack. Such blocking should be done only on very top of each stack, to prevent termination of application, also in the main cycle of event-oriented UI (such as inside Application.Run, the libraries provide special events and options to handle thread exceptions on this level). In almost all other processing exception should be re-thrown again throw throw, optionally, low-level exception can be caught, and semantic-aware exception thrown (the caught exception should be placed in InnerException of the newly created exception).

Exclusions from these recommendations are relatively rare; usually, blocking of all or some exception from propagation up the stack is used as a work-around of some defects in the some code, when its source code in not available for fixes.

—SA
 
Share this answer
 
Comments
Abhinav S 24-Feb-11 1:48am    
Good answer. As usual. 5. :)
Sergey Alexandrovich Kryukov 24-Feb-11 1:50am    
Thank you.
--SA
Nuri Ismail 24-Feb-11 3:20am    
Great explanation! 5+
Sergey Alexandrovich Kryukov 24-Feb-11 3:21am    
Thank you. It needs more, but it would take more than quick answer.
--SA
Espen Harlinn 24-Feb-11 4:11am    
Good points, my 5

I'll just add my 2 cents:
@Yatin Bhagat:
Sometimes it might be appropriate to silently, catch exceptions and continue execution, but that's very rare. Whenever you feel like doing this, carefully think through the consequences – at least consider logging the exception information

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