When to catch exceptions?

Will there be any difference. Should 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; } } 
+5
c ++ exception-handling
Nov 05 '10 at 10:30
source share
9 answers

I would say that you should advise against using exceptions to control flow. Exceptions, as the name implies, are designed to handle exceptional circumstances. In the above case, you explicitly expect n to be> 5, so this is not really an exceptional circumstance. If there is a way for your application to handle this, then it should do this to raise an exception.

I am sure that there are cases when this logic falls, but in general I think a good rule of thumb.

But in technical terms there isn’t much difference (maybe performance if you do it a lot).

+8
Nov 05 '10 at 10:35
source share

Never throw an exception that you then catch in the same function. This is a sign that you are using exceptions for the standard control flow, which is best done with if / while / break / etc.

+3
Nov 05 '10 at
source share

The end result will be the same as for sure.

You should try to simplify as much code as possible, so I strongly discourage the use of exceptions in this case.

+2
Nov 05 '10 at
source share

It is very difficult to say exactly when to use exceptions. In some cases, exceptions are a clear winner, and in other cases they are not.

The core of the question is: where does n come from and can under normal circumstances be a value> 5. If n is calculated by the function itself and usually can have this value, then the exception willn’t feel good. If n is indicated elsewhere, and this function simply doesn’t expects a high value, then the exception will be more correct.

However, your example, I would say, makes poor use of exceptions. The throwing and trap of exception within the same function is almost always bad. This is standard flow control. Exceptions should be used when an error condition should propagate outside the function.

+2
Nov 05 '10 at
source share

In your example, there is no real difference (apart from the obvious fact that one uses exceptions and the other does not!) - that would be a reasonable refactoring. However, if there are many different error conditions, you can find a throw... catch pattern that helps you handle errors in one place.

0
Nov 05 '10 at
source share

There is no difference in your example. The only thing you need to find out is that when an exception is thrown, the rest of the statements found insist that the try ... catch method will never be executed. Exceptions are mainly used to handle "special conditions that change the normal flow of program execution." (your just normal logical error stream).

Hope this helps.

0
Nov 05 '10 at
source share
 void func() { if (n > 5) { MessageBox(0, "n is greater than 5", "Error", 0); return; } } 

Do not throw an exception on your own if you can handle it on your own, for example, with the code above, its not a good practice.

0
Nov 05 '10 at
source share

You have not defined n .

In the following definition of n there is a 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 and hth.,

0
Nov 05. '10 at 10:57
source share

Much has been said, I’ll just add something on my part.

In your case, both cases are true, except that I would like to divide it into two levels: logic and representation. So your logic level is:

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

and your viewing level can do:

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

But then again, as another said: the most important thing is where the Russian comes from. If you expect it to be greater than 5, just use if () else, not the exception. But if n is always less than 5, and if it is more than 5, then something is wrong with your system, use exceptions.

0
Nov 05 '10 at
source share



All Articles