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
Solved

Custom Exception Help

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

839 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