[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Custom Exception Help

Posted on 2007-11-21
1
Medium Priority
?
190 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month20 days, 6 hours left to enroll

872 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