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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:52:08+00:00 2026-05-24T07:52:08+00:00

What is the best way to handle a function that might occasional fail to

  • 0

What is the best way to handle a function that might occasional fail to evaluate, but even when it does fail does not require a halt of the parent routine, though sometimes might require an explanation to the user?

I have such a function, deep in my code, that returns a number. Codingwise, the simplest approach would be to make the function nullable, and return null when it can’t evaluate. That way the calling routine can continue while also knowing the function failed to return a number.

However, depending upon the circumstances, sometimes I want to display the reason for the fail to the user so they know what to fix. Obviously just returning null isn’t enough information. Should I be raising messages inside the function itself as it evaluates for anonymous listeners to catch, to be displayed if desired?
(For those rightfully pointing out that logic functions shouldn’t be responsible for creating user messages, I didn’t mean to imply the function generates the full text message, just somehow transmits the reason for the problem so that the UI could later parse that into a message)

The other option I suppose would be throw exceptions inside the function if it can’t evaluate, then catch and interpret to user messages if desired. However, as stated the inability to evaluate typically does not signify a halt to the routine, and now I’d have to put a Try…Catch block around the function call everytime I use it.

  • 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-24T07:52:08+00:00Added an answer on May 24, 2026 at 7:52 am

    You can pass out parameter “string for example” to the function, so whenever the function failed, by returning false or null print the cause to the user. Just like what Microsoft do with TryParse.. but here we are getting the reason of the fail as well:

    public bool TrySomeFunction(out string errorMessage) 
    {
        try
        {
            //code that may cause exception
    
            return true;//operation completed successfully
        }
        catch (Exception exception)
        {
            errorMessage = exception.Message;
        }
    
        return false; 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way to handle a big team that has access to
What would be the best way to handle the __repr__() function for an object
Is it true that the only way to handle default function arguments is through
What is the best way to handle mixing PHP into Javascript? Should it not
What's the best way to handle translations for stock text in a SSRS .
What's the best way to handle a visitor constructing their own URL and replacing
Is this the best way to handle file moving in a windows service? we
What's the best way to handle data entered through dynamically generated controls in ASP.NET?
What is the best way to handle several different onload scripts spread across many
What is the best way to handle unexisting actions in the Zend Framework? Depending

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.