I just started learning how to handle errors with C ++, but before that I was familiar with java. This is where the construction works:

try { int d = 2/0; } catch(ArithmeticException t) { System.out.println("Error!!!"); } 

And immediately I wanted to implement something like that in c ++, but to my regret I did not find any ready-made classes, except for exception . Can you please tell me where I couldn鈥檛 see where there are any ready-made constructions / classes? How to implement a similar design on the pros? Is it possible to create exceptions at all without the throw keyword?


If there are links with great / good material to handle errors in c ++, please drop it. Do I understand correctly that in c ++ there is nothing at all ready, as in java, and everything has to be done with pens?

  • In c ++, any object can be thrown away as an exception. For example, throw new int(10) . Generally, exceptions are not very peculiar with ++, in order to catch the NPE, you need to write non-trivial platform dependent code ... Of the frequent exception, bad_aloc when memory is not allocated, I don鈥檛 even remember the others right away ... But with method, first check and then do not the other way around - pavel
  • The hierarchy of exception classes in C ++ 11 is here: cplusplus.com/reference/exception/exception - andy.37
  • In general, besides the standard, there are a number of good practices and the language has its own ideology. For exceptions you can see here: isocpp.org/wiki/faq/exceptions - user1056837

2 answers 2

Specifically on the design in your question: in C ++, this exception cannot be caught, for this you have to use the platform-dependent API, since division by 0 is the processor's exception (trap).

If we speak in general, then in C ++ there are such base classes that can either be used directly or inherited from them and already produce our own exception classes, i.e. in this regard, the differences from Java are minimal. But it is not necessary to inherit at all - any type can be used as an exception, i.e. There are no special exception types, there are just types that can be used to create and catch exceptions.

You can also add that it is desirable to inherit all exception classes in an application from one class, so that you can catch all exceptions all at once in one place (for example, in main ). Because all standard exception classes inherit std::exception , inheriting your own from it can be a good idea.

  • I would add that it is not even necessary to inherit your own exception classes from std::exception , although it is desirable. - andy.37
  • @ andy.37, and why is it desirable? Is there nothing but method what? - chebakov-grisha
  • @ chebakov-grisha in general, no why, except for a certain uniformity of the code, IMHO. I would like to use all exceptions in a similar way. - andy.37
  • @ chebakov-grisha So no one bothers you to build your hierarchy. But, on the other hand, you can, as a last resort, intercept whatever happens in main() and at least get at least something similar to intelligible information about what happened ... PS I highly recommend taking Sutter - the same "Solution complex tasks in C ++ "and" New challenges "and read the sections on exceptions. - Harry

I'll start from afar. Let's look at the standard language library. It immediately catches the eye that exceptions are almost not used there, especially when compared with Java, where it is customary to throw exceptions for each. For example, look at the description of the std::copy algorithm:

This is not the case. [First, last)

What happens if we do not fulfill this condition?

Note that invalid arguments cause undefined behavior.

And the devil knows. Maybe an exception, maybe an infinite loop, or maybe formatting a hard disk. In short, undefined behavior.
In the standard library, it is not customary to prohibit a programmer from shooting himself in the foot if he wants to.
As for your example, with this approach it would look like this:

 double div(double x, double y){ return x/y; } 

And let the correctness of the input data remains on the conscience of the one who called this function.

Now directly to your question. There is no standard exception when dividing by zero. That's all there is . If you really want to do the division with exceptions, you can write

 class DivisionByZeroException{}; double div(double x, double y){ if(y == 0){ throw DivisionByZeroException(); } return x/y; } 

And then use this feature. However, you strongly do not recommend it. From exceptions in C + + more problems than good. They are slow, exceptions in constructors lead to memory leaks, exceptions in destructors lead to undefined behavior, exceptions in methods with the noexcept specifier lead to program crashes, exceptions during exception handling also lead to program crashes.

  • Can you explain when an exception from the constructor leads to a memory leak? And are you sure that stack deployment is always slow? - user1056837
  • one
    The real division of non-zero by 0 is quite a valid one. - 伪位蔚蠂慰位蠀蟿
  • one
    @ user1056837, example - yrHeTaTeJlb
  • @yrHeTaTeJlb No, of course, you can take and shoot yourself in the foot, and then blame the gun ... You should not protect the language from every sneeze - because if you create a system that any fool can use - only a fool will use it. PS And your arguments against the use of exceptions - to be honest, they remind you "you are not able to prepare them" :) Do not confuse the undecided youth! - Harry
  • The example with leaks is largely unrelated to the constructor, this can be done in any function. You just have to stick to RAII and everything will be fine. - 伪位蔚蠂慰位蠀蟿