1

I have a control flow question. In my company we create a lot of bool methods that return false if there was an error. Example:

public bool Foo(string path, string fileName, ref string error)
{
    if (path == null)
    {
        error = "path is null";
        return false;
    }
    path += fileName;
    return true;
}

As you can see it's ugly. I want to use it with exceptions like so:

public voidFoo(string path, string fileName, ref string error)
{
    if (path == null)
    {
        throw new SomeException("Path is null.");
    }
    path += fileName;
    return true;
}

But we worry about the overhead. Should we?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
guyl
  • 2,158
  • 4
  • 32
  • 58

7 Answers7

3

If the exception is not thrown, then the overhead of your try...catch is negligable. So, the rule of thumb is:

  • If the exception is likely to be thrown (i.e., if path == null is a "supported" scenario), use the return value.
  • If the exception is unlikely, i.e., if path == null usually only happens if the developer using your function makes a mistake, then use an exception.
Heinzi
  • 167,459
  • 57
  • 363
  • 519
2

You should never use exceptions as a form of flow control.

In general, throwing exceptions is much more expensive than checking for a fail value - in this case, if path should never be false and is expected to always exist, an empty path is an exceptional situation and an exception should be thrown.

In term of method design - you shouldn't rely on the calling method to check a return value. If someone forgets to check, what will happen if a false is returned? An exception makes this issue go away, as something bad clearly happened and your code stops running.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
1

How exceptional is this occurrence? Throwing and catching exceptions will incur an overhead that you may not be willing to take in general usage.

Stylistically, using exceptions for flow control is frowned upon, both for performance and comprehension reasons. I would use exceptions if this is (no pun) an exceptional occurrence. If performance is key (and you should measure this - premature optimisation being the root of all evil, etc.) then a more optimal solution would be to use the return value.

The headache with such scenarios is that it's possible to ignore or misuse the return value (being false or null, usually).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
1

Exceptions should be reserved for exceptional cases such as a resource is unavailable E.g. disk space or a network connection.

Using exceptions for flow control is simply wrong, it doesn't smell right.

Using a boolean return code is one way. You could also create an error object that describes the cause of the error rather than returning via a ref.

Fortyrunner
  • 12,702
  • 4
  • 31
  • 54
0

All books say that you should use exceptions for signalling errors. If you use if's then 50% of your source lines are about return error messages and it is hard to read the source and see whether it's bugless.

Use return values only in time critical scenarios where you expect many errors to occur and computational time is very important for you.

Al Kepp
  • 5,831
  • 2
  • 28
  • 48
0

I love exceptions. I just do. Of course it depends on where you are implementing.

For example, I would like to have some very basic classes to just throw an exception, because I don't want these classes to have the responsibility of not throwing it.

Nice thing of C# is that you don't have to catch every exception, because it just throws it further if it goes wrong. So that will save you a lot of code. Else with booleans it would be: if false -> return false; if false -> return false;. I think that would be overhead.

Of course at some point, you have to catch your exception, but that is of the choice of the implementer where to do it. You don't want some giant catch block of course, but only at the point where you want to catch it, you should catch it.

I'd go for it.

Marnix
  • 6,384
  • 4
  • 43
  • 78
0

It depends on whether what you consider the "false" state to be exceptional or not. Or in other words not generally expected.

For example, if you pass in an object to a method that does not make any sense if it is null for the method to do its work, then perhaps you should be throwing an ArgumentNullException:

public void Foo(object obj)
{
    if(obj == null) throw new ArgumentNullException("obj", "Object cannot be null.");

    //
}
Andy
  • 5,188
  • 10
  • 42
  • 59