Is try catch is using a good coding [Resolved]

Posted by krrishbiju-15589 under C# on 12/2/2013 | Points: 10 | Views : 1077 | Status : [Member] | Replies : 5
Hi,
Can anyone help me one this,

Is using try catch is good experience in coding practice......
If its not please suggest an alternate way of handling exceptions.

Thanks and Regards
krrish




Responses

Posted by: Allemahesh on: 12/2/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
Best practice is that exception handling should never hide issues. This means that try-catch blocks should be extremely rare.

There are 3 circumstances were using a try-catch makes sense.

1. Always deal with known exceptions as low-down as you can. However, if you're expecting an exception it's usually better practice to test for it first. For instance parse, formatting and arithmetic exceptions are nearly always better handled by logic checks first, rather than a specific try-catch.
2. If you need to do something on an exception (for instance logging or roll back a transaction) then re-throw the exception.
3. Always deal with unknown exceptions as high-up as you can - the only code that should consume an exception and not re-throw it should be the UI or public API.

If this helps you towards the solution, please click on MARK IT AS ANSWER link.

Happy Coding.

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/2/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Try-Catch must be implemented in coding because it tells you from where exception has occurred.
Sometimes it's required to by-pass an exception So that your application will be intact.
You can show the message like duplicate record entered,if same record is adding. You can get this by using SQException class.
So it's good practice to handle exception in our code.

You can create your own exception class as Application exception.
You can create a Log class to store any error message either into Table or into File.


krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 12/2/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer
http://stackoverflow.com/questions/14973642/how-using-try-catch-for-exception-handling-is-best-practice
http://msdn.microsoft.com/en-us/library/seyhszts(v=vs.110).aspx
http://stackoverflow.com/questions/18648987/try-catch-best-practices-for-optimal-performance

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 12/2/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear krrish,

Have you got the solution?

If this helps you towards the solution, please click on MARK IT AS ANSWER link.
Happy Coding.

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajesh4383 on: 12/2/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Krish,

Please find below link for coding standard you will get more detail:

http://rajeshprajapati.blogspot.in/2013/12/dot-net-naming-conversions-and-style.html

Thanks
Rajesh

krrishbiju-15589, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response