Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does 'throw' or 'try...catch' hinder performance? [duplicate]

Tags:

c#

exception

Possible Duplicate:
How slow are .NET exceptions?

I've been reading all over the place (including here) about when exception should / shouldn't be used. I now want to change my code that would throw to make the method return false and handle it like that, but my question is: Is it the throwing or try..catch-ing that can hinder performance...? What I mean is, would this be acceptable:


bool method someMmethod()
{
    try
    {    
        // ...Do something
    catch (Exception ex) // Don't care too much what at the moment...
    {
        // Output error
        // Return false
    }
    return true // No errors

Or would there be a better way to do it? (I'm bloody sick of seeing "Unhandled exception..." LOL!)

like image 904
Richard Avatar asked Jun 23 '26 10:06

Richard


2 Answers

Ask yourself the following question: Is the exception exceptional?

  • If this can happen in normal program flow, for example, a failure to parse a number typed by the user, don't use an exception.
  • If this should not normally happen, but rather signifies a problem outside the program's control, such as a missing file, use an exception.
like image 65
Thomas Avatar answered Jun 26 '26 01:06

Thomas


If your question is whether or not the presence of a try...catch block will affect performance, then no.

If your question is whether there is a performance hit in using an exception-based model rather than a return value model, then yes, there is. Having a function like this:

public void DoWork()
{
    if(something) throw new Exception(...);
}

Is not going to perform as well under error conditions as a function like this:

public bool DoWork()
{
    if(something) return false;

    return true;
}

Exceptions have to unwind the stack and kick you out to the nearest catch block in order to work, so there's overhead involved in that. It's simpler to return a status value, but it's also a more choppy interface to deal with when exceptions are not the rule.

However, that isn't the point. If you're writing code where exceptions are the rule, then you have a problem. Exceptions should be used in...exceptional...conditions, such as when you encounter a condition that you could not account for in code.

Consider the types like int and DateTime. These types provide (among others) two different functions for converting string values into corresponding int and DateTime values: Parse and TryParse. Parse uses the exception model, since it's assuming at that point that you'll be passing it a well-formed integer value, so if it gets something else, that's an exceptional condition. TryParse, on the other hand, is designed for when you are not sure about the format of the string, so it uses the return value model (along with an out parameter in order to get the actual converted value).

like image 26
Adam Robinson Avatar answered Jun 26 '26 01:06

Adam Robinson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!