Solved

Use of Try/Catch with c# 2010

Posted on 2010-09-07
6
352 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 20

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

773 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