Solved

Custom Exception Help

Posted on 2007-11-21
1
183 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

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