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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:56:59+00:00 2026-05-26T13:56:59+00:00

What strategy do you use to give to the user the reason why a

  • 0

What strategy do you use to give to the user the reason why a certain method “failed”

Exemple:

public List<Balance> GetBalanceFinale(Periode periode)
    {
        if (periode == null || periode.DateStart >= DateTime.Now || isBalanceFinished(periode.PeriodeID))
            return null;

        //My other code...
     }

I want to tell the user which of the steps went wrong. I don’t want to use a messagebox in such class. I can’t return the description of the failure because I already return something.

What do you usally do? Any advice? Thanks!

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

    I am assuming you don’t want to throw an exception otherwise you would’ve already done that. Something like an alert / warning without stopping execution of the program. In that case, you can still use an exception, just don’t throw it, instead pass it as an out parameter or put it somewhere where the user can access it if desired. If that seems over the top then just use a message instead.

    Also framing it as a ‘Try’ method might be a good idea. It makes it very clear that the method is prone to failure under certain conditions.

    These are all different options:

    public bool TryGetBalanceFinale(Periode periode, out List<Balance> list, out string msg)
    {
        // return false if anything is wrong, and have an out parameter for the result & msg
    }
    
    public bool TryGetBalanceFinale(Periode periode, out List<Balance> list, out Exception ex)
    {
        // return false if anything is wrong, and have an out parameter for the exception
    }
    

    These first two above are my two preferred approaches. The following are possibilities as well, however they are somewhat non-standard:

    public Tuple<string, bool> TryGetBalanceFinale(Periode periode, out List<Balance> list)
    {
        // return false if anything is wrong, and include message in the returned Tuple
    }
    
    
    // an anonymous type approach
    public object TryGetBalanceFinale(Periode periode, out List<Balance> list)
    {
        return new {
           Successful = false,
           Message = // reason why here
        };
    }
    
    // a functional approach
    public List<Balance> list GetBalanceFinale(Periode periode, Action<String> messageAct)
    {
       // when something is wrong, do:
       messageAct("Something went wrong..."); 
    }
    

    I think the ‘Try’ strategy makes the most sense when you consider how it will be used:

    string message;
    List<Balance> result;
    
    if (!TryGetBalanceFinale(periode, out result, out message))
    {
        // examine the msg because you know the method failed
        Console.WriteLine(message); 
    }
    else
    {
        // you know the method succeeded, so use the result
        Console.WriteLine("The result is: " + result.ToString()); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it normal to use default strategy like in my code below: public abstract
What's the best strategy to use when writing JMeters tests against a web application
What strategy should I use if I have an implementation of std::fstream with 32-bit
I have a class which is going to need to use the strategy design
At work we use a branching strategy where all changes start off in a
In my test automation practice I always use a gui mapping strategy that reduces
I have some questions about partitioning and strategy, how and when to use it.
@Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) public class Problem { @ManyToOne private Person person; } @Entity
I have a naming strategy for denoting the nature of code entity (variable, method,
What is a good deployment strategy to use with Git + Heroku (Ruby on

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.