Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 720269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:44:20+00:00 2026-05-14T05:44:20+00:00

While searching SO for approaches to error handling related to business rule validation, all

  • 0

While searching SO for approaches to error handling related to business rule validation, all I encounter are examples of structured exception handling.

MSDN and many other reputable development resources are very clear that exceptions are not to be used to handle routine error cases. They are only to be used for exceptional circumstances and unexpected errors that may occur from improper use by the programmer (but not the user.) In many cases, user errors such as fields that are left blank are common, and things which our program should expect, and therefore are not exceptional and not candidates for use of exceptions.

QUOTE:

Remember that the use of the term
exception in programming has to do
with the thinking that an exception
should represent an exceptional
condition. Exceptional conditions, by
their very nature, do not normally
occur; so your code should not throw
exceptions as part of its everyday
operations.

Do not throw exceptions to signal
commonly occurring events.
Consider
using alternate methods to communicate
to a caller the occurrence of those
events and leave the exception
throwing for when something truly out
of the ordinary happens.

For example, proper use:

private void DoSomething(string requiredParameter)
{
if (requiredParameter == null) throw new ArgumentExpcetion("requiredParameter cannot be null");
// Remainder of method body...
}

Improper use:

// Renames item to a name supplied by the user.  Name must begin with an "F".
public void RenameItem(string newName)
{
   // Items must have names that begin with "F"
   if (!newName.StartsWith("F")) throw new RenameException("New name must begin with /"F/"");
   // Remainder of method body...
}

In the above case, according to best practices, it would have been better to pass the error up to the UI without involving/requiring .NET’s exception handling mechanisms.

Using the same example above, suppose one were to need to enforce a set of naming rules against items. What approach would be best?

  1. Having the method return a
    enumerated result?
    RenameResult.Success,
    RenameResult.TooShort,
    RenameResult.TooLong,
    RenameResult.InvalidCharacters, etc.

  2. Using an event in a controller class
    to report to the UI class? The UI calls the
    controller’s RenameItem method, and then handles an
    AfterRename event that the controller raises and
    that has rename status as part of the event args?

  3. The controlling class directly references
    and calls a method from the UI class that
    handles the error, e.g. ReportError(string text).

  4. Something else… ?

Essentially, I want to know how to perform complex validation in classes that may not be the Form class itself, and pass the errors back to the Form class for display — but I do not want to involve exception handling where it should not be used (even though it seems much easier!)


Based on responses to the question, I feel that I’ll have to state the problem in terms that are more concrete:

UI = User Interface, BLL = Business Logic Layer (in this case, just a different class)

  1. User enters value within UI.
  2. UI reports value to BLL.
  3. BLL performs routine validation of the value.
  4. BLL discovers rule violation.
  5. BLL returns rule violation to UI.
  6. UI recieves return from BLL and reports error to user.

Since it is routine for a user to enter invalid values, exceptions should not be used. What is the right way to do this without exceptions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:44:21+00:00Added an answer on May 14, 2026 at 5:44 am

    The example you give is of UI validating inputs.

    Therefore, a good approach is to separate the validation from the action. WinForms has a built in validation system, but in principle, it works along these lines:

    ValidationResult v = ValidateName(string newName);
    if (v == ValidationResult.NameOk)
        SetName(newName);
    else
        ReportErrorAndAskUserToRetry(...);
    

    In addition, you can apply the validation in the SetName method to ensure that the validity has been checked:

    public void SetName(string newName)
    {
        if (ValidateName(newName) != ValidationResult.NameOk)
            throw new InvalidOperationException("name has not been correctly validated");
    
        name = newName;
    }
    

    (Note that this may not be the best approach for performance, but in the situation of applying a simple validation check to a UI input, it is unlikely that validating twice will be of any significance. Alternatively, the above check could be done purely as a debug-only assert check to catch any attempt by programmers to call the method without first validating the input. Once you know that all callers are abiding by their contract, there is often no need for a release runtime check at all)

    To quote another answer:

    Either a member fulfills its contract or it throws an exception. Period.

    The thing that this misses out is: What is the contract? It is perfectly reasonable to state in the “contract” that a method returns a status value. e.g. File.Exists() returns a status code, not an exception, because that is its contract.

    However, your example is different. In it, you actually do two separate actions: validation and storing. If SetName can either return a status code or set the name, it is trying to do two tasks in one, which means that the caller never knows which behaviour it will exhibit, and has to have special case handling for those cases. However, if you split SetName into separate Validate and Store steps, then the contract for StoreName can be that you pass in valid inputs (as passed by ValidateName), and it throws an exception if this contract is not met. Because each method then does one thing and one thing only, the contract is very clear, and it is obvious when an exception should be thrown.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to spring framework....while searching on google..I found few examples which has
Been searching for a while on this subject without any success. All I can
While searching the web, I found this code: $apprequest_url =https://graph.facebook.com/ . /apprequests?ids=USERID_1,USERID_2,USERID_3 . &message='INSERT_UT8_STRING_MSG'
While searching for a proper way to trim non-breaking space from parsed HTML, I've
While searching a bug in my code today I found a strange thing. When
While searching for some functions in C++ standard library documentation I read that push
I remember reading something once, but could not find it now while searching, if
I successfully migrate Joomla from 1.0 to 1.5.0 stable, while searching for migrate 1.5.0
While I am searching for better method to exit a Swing Application in between
I'm searching a while for this and I can't found something that works for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.