Solved

Use of Try/Catch with c# 2010

Posted on 2010-09-07
6
350 Views
Last Modified: 2013-12-17
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?
0
Comment
Question by:dbdp
6 Comments
 
LVL 19

Expert Comment

by:Daniel Van Der Werken
ID: 33616895
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.
0
 
LVL 14

Expert Comment

by:Daniel Junges
ID: 33616920
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
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 33616995
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.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 9

Expert Comment

by:puru1981
ID: 33617531
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.
0
 
LVL 16

Accepted Solution

by:
Vikram Singh Saini earned 500 total points
ID: 33618230
SPECIFIC EXCEPTION V/S GENERIC EXCEPTION:

(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.

Regards,
VSS
0
 

Author Closing Comment

by:dbdp
ID: 33660491
Very detailed yet easy explanation!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows Service with UDP 2 28
Duplicate a row 2 33
Showdialog 8 20
Calling stored proc in EDMX 11 19
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now