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

  • Home
  • SEARCH
  • 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 7516611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:11:13+00:00 2026-05-30T01:11:13+00:00

I was recently told that I’m abusing exceptions to control the flow in my

  • 0

I was recently told that I’m abusing exceptions to control the flow in my applications, so I this is my attempt to somehow clarify the situation.

To my mind, a method should throw an exception, when it encounters a situation, which can’t be handled internally or might be handled better by the calling side.


So – does any particular set of rules exist, which can be used to answer the following set of question when developing your applications:

  • When should I throw an exception and when should I write code with strong nothrow guarantee, which might simply return bool to indicate success or failure?

  • Should I try to minimize the number of situations, when the method throws an exception or , on the contrary, should it be maximized to provide flexibility when handling these situations?

  • Should I stick to the exception throwing convention set by the frameworks / runtimes I use when developing my applications or should I wrap all these calls so that they match my own exception throwing strategy?

  • I was also adviced to use error codes for error handling, which seems pretty efficient, but ugly from the syntactical point of view (also, when using them a developer loses the ability to specify the output for a method). What do you think about this?


Example for the third question (I was using an I/O framework and encountered the following situation):

The described framework does not use exceptions to handle errors, but
the other code does use them. Should I wrap every possible failure
indicated with '???' and throw an exception in this case?
Or should I change the signature of my method to bool
PrepareTheResultingOutputPath
and only indicate whether the operation was
successful or not?

public void PrepareTheResultingOutputFile(
    String templateFilePath, String outputFilePath)
{
    if (!File.Exists(templateFilePath))
        // ???

    if (!Directory.MakePath(outputFilePath))
        // ???

    if (File.Exists(outputFilePath))
        if (!File.Remove(outputFilePath))
            // ???

    if (!File.Copy(templateFilePath, outputFilePath)
        // ???
}

Another example – even the .NET Framework doesn’t follow some strict exception throwing strategy. Some methods are documented to throw 10+ different exception types, including trivial exception types like NullArgumentException, but some of them simply return bool to indicate success or failure of the operations.

Thank you!

  • 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-30T01:11:14+00:00Added an answer on May 30, 2026 at 1:11 am

    The problem with exceptions is that they are essentially glorified gotos that have the ability to unwind the program’s call stack. So, if you are “using exceptions for flow control,” you are probably using them as gotos rather than as indications of an exceptions condition. That’s exactly the point of exceptions, and the reason for their name: they are supposed to be used only in exceptional cases. So, unless a method is designed not to throw an exception (an example is .NET’s int.TryParse), it’s OK to throw an exception in response to exceptional circumstances.

    The nice thing about C# as opposed to Java is that in C# you can essentially return two or more values, by returning a tuple type or by using out parameters. So, there isn’t much ugliness in returning an error code as the method’s main return value, since you can use out parameters for the rest. For example, the common paradigm for calling int.TryParse is

    string s = /* Read a string from somewhere */;
    int n;
    if (int.TryParse(s, out n))
    {
        // Use n somehow
    }
    else
    {
        // Tell the user that they entered a wrong number
    }
    

    Now for your third question, which seems to be the most substantial. In reference to your example code, you ask if you should return bool to indicate success/failure or if you should use exceptions to indicate failure. There is a third option, though. You can define an enum to tell how the method could fail, and return a value of that type to the caller. Then, the caller has a wide choice: the caller doesn’t have to use a bunch of try/catch statements, or an if that gives little insight into how the method failed, but can choose to write either

    if (PrepareTheResultingOutputFile(templateFilePath, outputFilePath) == Status.Success)
        // Do  something
    else
        // It failed!
    

    or

    switch (PrepareTheResultingOutputFile(templateFilePath, outputFilePath))
    {
        case Status.Success:
            // Do something
            break;
        case Status.FileNotPresent:
            // Do something else
            break;
        case Status.CannotMakePath:
            // Do something else
            break;
        // And so on
        default:
            // Some other reason for failure
            break;
    }
    

    You can find more on this issue here and here, but especially in Joel Spolsky’s post, which I highly recommend.

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

Sidebar

Related Questions

This is a (hopefully) really simple question - I have been told recently that
I recently found an article online that told me about this: RewriteRule ^mock-up/([^/]+)/([^/]+) /mock-up/index.php?page=$1&section=$2
I was recently told that I should use transactions in my application as they
The obvious answer is to use Charset.defaultCharset() but we recently found out that this
So I was recently doing some work, when somebody told me that if doing
Recently I have been told that static class/methods are evil. Take for example my
I was told recently by a Flash developer that I respect that using include
I was told recently (on here) that concatenating your Javascript will cause XSS vulnerabilities.
I recently told a friend that I was starting to learn Catalyst (Perl) and
I originally asked this question on Super User but was told that it might

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.