Use of Try/Catch with c# 2010

I am using c# 2010 and have religiously put a catch for all possible exceptions in a method, followed by the most generic (Exception Ex)

Is this really necessary, if I just use the generic cathall it still seems to give me correct error so should I continue using all possible exceptions or not?

Any advice?
Who is Participating?
Vikram Singh SainiConnect With a Mentor Software Engineer cum AD DeveloperCommented:

(1) In favor of Specific Exception:
If you want to provide your user specific information (which could be of use to user) then it is best to include specific exception. For e.g. in your application a number would be divided by zero by user, in that case you have set generic exception, that would result to user DivideByZeroException, but if you want to let your user know that he/she should divide that number with any other number not by zero (for this you would have to catch this exception).

(2) In favor of Generic Exception:
Now if you have included many of catch() blocks with single try block for handling exception, the CLR continues to check each catch block, until it doesn't found specific exception, otherwise the generic exception would handle it. But lot of catch causes your runtime to spend time on checking them, and hence little bit slower execution of program.

(3) In favor of Specific Exception:
You are developing a big project (or software) & you need to deliver same to your client at distant location, you have implemented only generic exceptions in your program for handling exceptions. Now when your user experience some exception he/she is unable to resolve it independently. Why? Because there is no useful information. In that case, he would need to contact you, and ask for it. Then you would ask for full detail resulting in wasting of time that could be use in other productive tasks. So here comes the usefulness of Specific Exception.

For e.g. exception reports NullException which means object reference not set to an instance of object. And this could be caused by either some mistake in your code or might be your program depends on user's system's specific file. And somehow that file is not in system or deleted by user. So better to let user check if that file is there or not...

(4) In favor of Generic Exception:
Generic exceptions would be nice if you are developing web application, so that the same can be reported or log in some file. And you can check the same file back to resolve your web application. And you do not want to include specific exception in order to prevent time.

Hope it helps you to understand which one to use and when to use.

Daniel Van Der WerkenIndependent ConsultantCommented:
Generally (pun intended) the general exception is all you need.  If you know you might be hitting a specific exception and want to get that specific info, then catch the specific exceptions.
Daniel JungesCommented:
you can use the cathall normally, but if you want to hadle a specific exception then catch first the specific exeption.

Case: supose that you have a block of and inside you work with a file and work with database, then if you want to handle the file error then catch the fileexception before the gereric catchall
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Carl TawnSystems and Integration DeveloperCommented:
The answer (as with so many programming tasks) is "it depends". If you simply want to trap exceptions and either log/report them to the user then you can just catch the generic Exception. If it's something that needs special handling (such as an operation you can re-try if it fails, or switch to a failover server, etc) then you will want to catch a more specific exception type.
i would prefer to use the exceptions and give a user useful interpretation what can be done to rectify the issue using the different type of exceptions. After that i will write a generic exception and log it as unhandled exception. just giving an example if i am using a webservice then may be the server is down or some network is busy, then i can show the user that try after some time when server is up or if the network bandwidth issue then ask him to do the heavy processing when the load on the server is less etc.
dbdpAuthor Commented:
Very detailed yet easy explanation!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.