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

x
?
Solved

Custom Exception Help

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

650 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