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 6100175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:21:22+00:00 2026-05-23T13:21:22+00:00

In my programmer’s experience, I have mixed error handling all the ways possible… I

  • 0

In my programmer’s experience, I have mixed error handling all the ways possible… I have created my personal style.

However, I’d like to hear what you consider to be the pro and cons of error handling at the beginning vs at the end of the method.

Handling at the beginning:

public String GenerateSomeStringData(String data, int value)
{
    if (data == null)
        throw new ArgumentNullException("data");

    if (value <= 0)
        throw new ArgumentException("value must be greater than zero");

    int dataValue;
    if (!int.TryParse(data, out dataValue))
        throw new InvalidOperationException("data does not contain an integer");

    return dataValue * 4 + value / 12;
}

Handling at the end: (the same example)

public String GenerateSomeStringData(String data, int value)
{
    if (data != null)
    {
        if (value > 0) 
        {
            int dataValue;
            if (int.TryParse(data, out dataValue))
            {
                return dataValue * 4 + value / 12;
            }
            else 
                throw new InvalidOperationException("data does not contain an integer");
        }
        else 
            throw new ArgumentException("value must be greater than zero");
    }
    else 
        throw new ArgumentNullException("data");
}

What criteria do you use when deciding how to approach this? Readability, maintainability, brevity?

  • 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-23T13:21:22+00:00Added an answer on May 23, 2026 at 1:21 pm

    Validity of input should be a precondition for execution of the method – as such I would (and do) always do the error handling first.

    This has the following advantages:

    • It is easier to parse for a human:
      validating preconditions first, then
      execution logic (which usually
      results in some post condition)

    • Clear separation of concerns between
      error handling and execution logic
      within your method: validation logic is not “sprinkled in” within the execution logic

    As mentioned in the comments you have to differentiate between invalid input that violates a precondition and triggers an error condition (such as throwing an exception) and valid input that constitutes an edge condition (i.e. requiring some special logic to handle). The later case I would handle separately after asserting the preconditions, at the beginning of the execution logic of your method.

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

Sidebar

Related Questions

Beginner programmer here....hope it makes sense :) I have created a console app that
New programmer- I have created a function to add a user to a database.
We have a junior programmer that simply doesn't write enough tests. I have to
As a programmer I have no idea how one would go about programming menus
I am a programmer at a financial institute. I have recently been told to
As programmer I need to filter, search and validate data every day. For all
I'm an average programmer in C++, and usually avoid pointers at all costs. But
newbie programmer here. I have 3 tables namely product, category, and subcategory. I configured
The programmer does define what happens inside main() , after all. So, should it
i am a newbie programmer in ASP.NET. So My database have two tables: UserTypes

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.