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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:48:55+00:00 2026-05-11T19:48:55+00:00

A lot of the code base methods my peers have written perform their own

  • 0

A lot of the code base methods my peers have written perform their own error handling, usually by catching, notifying, and logging.

In these cases the methods return a boolean, indicating success or failure.

Sometimes though, if a method fails, I want the calling code to know why, and returning a boolean is not enough.

One way around this is to keep the error handling in the methods, but make the method void, and have the methods throw their Exceptions.

However, recently I’ve got into the habit of returning Exceptions in appropriate methods, usually action methods that could otherwise be void, but where I’d like to know the status of the action.

The advantages of returning an Exception over having a void method throw an Exception is that I think it makes it easier for other programmers to use your library, and, more importantly, it means you can safely call that method without having to worry about catching the Exception

For example, if the method is just void, it might not be instantly obvious that the programmer should handle success or failure.

But if the methods specifically specifies a return type of Exception, then you know you can check success or failure if you want to. But it also means you don’t need to worry about catching the error if you don’t want to.

Does that make sense? Maybe I haven’t used the best example, but generally, is it OK to return Exceptions, or is there a better pattern out there?

UPDATE

wow, the overwhelming result is no way. I thought so. I must say, doing it (returning an Exception) kinda solved a problem, but it did feel wrong.

So, from some of your answers, the best solution to these specific scenarios (a complex class, or a class with one or more external dependencies (i.e. web service)) seems to be a custom results Class?

UPDATE:

I really appreciate all the opinions, I am reading through everything, and I’m thinking carefully about all the input.

Currently I’m favoring having a void method, throwing the Exceptions, and then catching them on the outside….is that better?

  • 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-11T19:48:55+00:00Added an answer on May 11, 2026 at 7:48 pm

    If you mean something like …

    public Exception MyMethod( string foo )
    {
       if( String.IsNullOrEmpty() )
       {
          return new ArgumentNullException( "foo" );
       }
    }
    

    … rather than …

    public void MyMethod( string foo )
    {
       if( String.IsNullOrEmpty() )
       {
          throw new ArgumentNullException( "foo" )
       }
    }
    

    Then absolutely not, it is not okay to do that. You would be completely re-inventing the purpose of an exception and using it as an hresult. Here are some standard best practices.

    Another good reason not to is that standard delegates would no longer match your method signature. So, for example, you could not use Action<string> on MyMethod anymore and would need to use Func<string,Exception> instead.

    @Andy, per comment, answer too long for a comment: Not really. Don’t be so concerned about the caller. He should be being defensive in his design anyway. Think about the semantics of an Exception … “The execution of this application will stop right here unless somebody knows what to do about this problem.” If you can resolve the issue, you should resolve it. If you can’t, you have to log it and throw it to the next level, because they may know exactly what to do.

    You should handle what you can handle and throw what you can’t. By definition, the guy up the call stack has a broader view of the world than you. Your application needs to be resilient in it’s ability to deal with exceptions and keep on going. They only way to do that is defensive coding and push issues up to higher levels of information to see if anything can be done.

    At the end of the day if the answer is “no”, then log the problem to it can be understood, be a good citizen of the box and terminate the application gracefully and live to fight another day. Don’t be selfish and try and hide errors for your caller. Be defensive and he will do the same. 🙂

    Check out the Enterprise Library Exception handling block. It think they really articulate a great vision for how to deal with exceptions throughout your architecture.

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

Sidebar

Related Questions

DirectDraw 7 is very old APIs but we have a lot legacy code written
I have lot of code in my Tornado app which looks like this: @tornado.web.asynchronous
I am have a lot of code that I need to optimize and make
I have just refactored a lot of code out of my AccountController into an
In my project I have a lot of code like this: int[][] a =
I am reviewing another developer's code and he has written a lot of code
I find myself having a lot of this in different methods in my code:
In our code base public ActionResult Copy(string id, string targetId) { //lot of similar
I have a Perl codebase, and there are a lot of redundant functions and
A lot of code in a current project is directly related to displaying things

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.