开发者

When to catch exceptions?

开发者 https://www.devze.com 2023-01-23 21:01 出处:网络
Would there be any difference If i do the following without using exceptions? void func() { try { if (n > 5)

Would there be any difference If i do the following without using exceptions?

void func()
{
    try
    {
        if (n > 5)
        {
            throw "n is greater than 开发者_如何学运维5";
        }
    }
    catch (const char *e)
    {
        MessageBox(0, e, 0, 0);
        return;
    }
}

OR

void func()
{
    if (n > 5)
    {
       MessageBox(0, "n is greater than 5", "Error", 0);
       return;
    }
}


I would probably say that you best advised not to use exceptions for flow control. Exceptions, as the name suggests, are for handling exceptional circumstances. In the above case you're clearly expecting n to possibly be > 5 so it's not really an exceptional circumstance. If there is a way for your application to deal with that case, then it should do so in preference to raising an exception.

I'm sure there are cases where that logic falls down but in general I think that's a good rule of thumb.

But in technical terms there isn't much difference (possibly performance if you're doing it a lot).


Never throw an exception that you then catch in the same function. That's a sign that you're using exceptions for standard control flow, which is better done with if/while/break/etc.


The end result would be the exact same thing, that is for sure.

You should try to simplify things as much as possible in code, so I highly discourage usage of an exception in this case.


It's very hard to say exactly when exceptions should be used. In some cases exceptions are the clear winner, and in other cases they are not.

The core of the question is where does n come from, and can it be, under normal circumstances, a value > 5. If n is calculated by the function itself and can normally have this value then an exception doesn't feel right. If n however is specified elsewhere, and this function just doesn't expect a high value then an exception feels more correct.

However, your example I would say is a bad use of exceptions. Throwing and catching an exception within the same function is almost always bad form. This is standard flow control. Exceptions should be used when an error condition needs to propagate outside of the function.


There's no real difference in your example (aside from the obvious fact that one uses exceptions and the other doesn't!) - that would be a reasonable refactoring. However, if there are lots of different error conditions, you might find the throw... catch pattern helps you keep the error-handling in one place.


void func()
{
    if (n > 5)
    {
       MessageBox(0, "n is greater than 5", "Error", 0);
       return;
    }
}

Don't throw the exception yourself, if you can handle it on your own by a check like above code, Its Not a good practice.


In your example, there is no difference. The only thing you have to figure out is that when an exception is thrown, the rest of the statements found insided the try...catch method will never be executed. Exceptions are basically used to handle "special conditions that change the normal flow of program execution." (yours is just basic a normal logical error flow).

Hope this helps.


You didn't define n.

With the following definition of n there is different observable behavior:

struct Silly
{
    ~Silly() { cout << "Hm de dum" << endl; }
    operator bool() const { return true; }
};

struct SillyProducer
{
    Silly operator>( int ) const { return Silly(); }
};

#define n   Silly silly = SillyProducer()

Cheers & hth.,


A lot has been already said, I will just add something from my side.

In your case both cases are correct, except I would encourage to to split this into two layers: logic, and view. So your Logic layer would do:

    doLogic()
    {
      if (n > 5)
      {
         throw "n is greater than 5";
      }
      ///Something more
    }

and your view layer might do:

try
{ 
   doLogic()
}
catch (const char *e)
{
    MessageBox(0, e, 0, 0);
    return;
}

But again, as other said: the most important thing is where does the n come from. If you expect it to be more than 5 then just use if() else, not exceptions. But if n is always less than 5 and if it's more than 5 means that something is wrong with your system, then use exceptions.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号