Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Use of Try/Catch with c# 2010

Posted on 2010-09-07
6
Medium Priority
?
358 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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 2000 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

636 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