Error handling: return value and exception in C ++

In the process of requesting a "divide by 0" exception, I found that with C ++ we cannot do this. I mean, dividing by 0 does not raise std :: exception.

Some of the hints I found, I had to check the value and throw an exception myself.

I say this is confusing as I thought C ++ accepted the idea of ​​an exception to replace "a good old C / UNIX report error returning a value method".

Here are my questions

  • Q1: Why does C ++ not throw a std :: exception error to divide by 0? Is there a reason for this?
  • Q2: As a rule, what error handling scheme does C ++ users use? Always throw an error, and the exception is a division error by 0?
  • Q3: In general, OOP languages ​​prefer (or even force) to use an exception. Is it correct?
+7
c ++ oop exception-handling
source share
7 answers

C ++ assumes that you know what you are doing, don’t pay for what you don’t ask for, and don’t make any assumptions about the platforms for which it was intended.

If you want to divide the numbers, it would be very inefficient to ask the compiler to check the denominator and throw before division. (We did not ask for this.) So this option is missing; we cannot have this check on every division, and this is especially wasteful, since most divisions are not zero.

So, how can we just divide by zero and find out if this works? Since C ++ cannot accept something regarding this platform, it cannot assume that there is a way to check the result, in hardware. In other words, while many processors will switch to some kind of interrupt when division by zero occurs, C ++ cannot guarantee such a thing.

The only option is that the behavior should be undefined. And this is exactly what you get: undefined behavior.


OOP languages ​​can do something, it does not matter, since OOP is not well-defined, and C ++ is not an OOP language. In general, use the most suitable tool. (Exceptions for exceptional situations.)

+9
source share

C ++ is implemented on different platforms and is designed to support high-performance applications. Allowing undefined behavior means that not all uses of the separation must be burdened with additional validation and the possible throwing of an exception by the compiler. The compiler is allowed to implement the fastest translation of division into machine code, regardless of its behavior when divided by zero.

As with any operation, the responsibility of programmers is to ensure that any preconditions are met. In the case of division, the programmer may know that the divisor cannot be zero (or very small) and can simply use assert; in other cases, he may need to verify the entry and throw an application exception if the conditions are not met.

C ++ is not (simply) an OO language and does not (in most cases) force the use of exceptions. He provides them as a tool for use where necessary. There are other languages ​​that pretty much force the use of exceptions.

+2
source share

1) Throwing exceptions is an expensive operation. The C ++ philosophy does not have to pay for what you are not using. If you want exceptions, you throw them yourself (or use libraries that make).

2) Never accept division by zero error. It depends on the situation, if you know that the input will never be 0, never check it. If you are not sure, always check it. Then either throw an exception or swallow the error silently. It depends on you.

3) Throwing an exception, especially in combination with RAII, can make for really elegant and beautiful code. This may not be acceptable in all situations. You can have 100% confidence in your inputs and a desire for raw performance. If you are creating a DLL, you really do not want to throw exceptions from your api, but for a critically consistent statically linked library you will be recommended.

+2
source share

Division by zero is something you can check in front of a line of calculation that could avoid wasted cycles if it is a complex formula.

+1
source share

In some places, C ++ does not use many good principles to maintain compatibility with C code. Java and such do not have such restrictions, so they can do what they wanted.

In C ++, always throw an exception. But for something like dividing by zero, you really should just check it out yourself. This is not an exceptional circumstance; you cannot verify yourself.

+1
source share

About Q3 - exceptions are something that should happen exclusively :) Therefore, to avoid (which is possible with div0), it is always better. Also, Emyr (who's right about “avoiding wasted cycles to compute”), you should consider that throwing an exception means a lot of “internal work” as the context changes (you can leave the loop, functions, instance methods ...) and your excrement stack should be prepared.

In general, exception handling is a "general method" for handling exceptions. But this should not be a template to avoid a “value check”.

if (! string.IsNullOrEmpty (....) ... much better than trying {xx = MyMabeNullString.Length ...} to catch {// errmess - I could check it earlier :)}

+1
source share
  1. Because C ++ should be “close to metal” and basically trying to convey simple operations, such as dividing by hardware, relatively directly (so if you cannot depend on hardware to force restrict restrictions, it’s possible C ++ by there will be no default).
  2. I do not think there is a universal answer to this question. Some C ++ programmers write almost C-like code that almost never uses exception handling. Others use exception handling quite strictly (and most of them are somewhere in between).
  3. Although there is almost certainly a correlation between OO and exception handling, I don’t think it really causes or affects. Factors that seem likely to me:
    1. OOP and exception handling are generally most useful in similar (e.g. large) projects.
    2. OOP and exception handling have become more common over time. Older OO languages ​​often lack exception handling. Newer languages ​​often have exception handling, even if they are not OO at all.
+1
source share

All Articles