Solved

Custom Exception Help

Posted on 2007-11-21
1
181 Views
Last Modified: 2010-04-15
I have an application that has several types of custom exceptions.

Each method has several different types of exceptions that they can throw.

The code has 3 steps which are 3 places I want to catch exceptions:

ValidateParameters
Setup Account Info Object (Account service could be offline)
GetAccountInfo (accounInfo server could be down etc)

Just wondering the best way to do this, should I put a try catch block instead of the if statement or should I go ahead and place a try catch within Validate ParamDetails?

The I would assume just a try catch around the other 2 would do the job?

Cheers
//1. Validate Parameters

if (validateViolation1.ValidateParamDetails())

{

//2. Setup Account Info

AccountInfoServices.AccountInfo accountInfo = new AccountInfo();
 

//3. Account Info

accountInfo = accountInfoServices.GetAccountInfo();
 

}

Open in new window

0
Comment
Question by:directxBOB
1 Comment
 
LVL 40

Accepted Solution

by:
evilrix earned 500 total points
ID: 20326556
You should only catch exceptions at the point where you can actually do something sensible about them So, as a rule of thumb you should try and catch as early as possible but not so early as to mean the only sensible thing you could do would be to re-throw.

Only throw an exception on program errors -- exceptions are exceptional situations that are considered error conditions. Don't use exceptions to control program flow. So, for example, if failure to validate is a program error then throw an exception. If; however, it is a valid state then it is usually preferable to handle it as a return value.

I would say, from what I understand of your requirement, below would be a reasonable approach to take...
if (validateViolation1.ValidateParamDetails()) // Assuming failure to validate is not a program error

{

   try

   {

        AccountInfoServices.AccountInfo accountInfo = new AccountInfo();

        accountInfo = accountInfoServices.GetAccountInfo();

   }

   catch // Your catch handlers here

   {

        Handle the account info related exception as appropriate

   }

}

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

18 Experts available now in Live!

Get 1:1 Help Now