How to catch "any" exception in C ++?

I understand that all exceptions in C ++ ultimately extend exception . In the Java world, Exception e would work regardless of the type of exception. How is this done in C ++?

Why is there no exception in this fragment?

 try{ int z = 34/0; cout << "This line should not appear" << endl; } catch (exception e) { cout << "An error has occurred: " << e.what(); // Not executed } 

Also, in C ++, how do I know which actions are causing some kind of exception?

+4
source share
4 answers

Why is an exception thrown in this snippet?

An integer divided by 0 is not a standard C ++ exception. Thus, in this case, the exception that you get is not an exception. This is a simple Undefined Behavior .

Some specific compilers may map this scenario to a specific exception, and you will have to check your compiler documentation to find the same. However, using such a function will not be portable, and your code will be limited to your specific compiler.

The best thing you can do in such a scenario is to check the error condition (the divisor is zero) yourself and throw an exception explicitly.

Also, in C ++, how do I know which actions are causing some kind of exception?

The std::exception provides the std::exception::what() method specifically for this.

+5
source

Dividing by 0 leads to the fact that most processors follow some kind of escalation procedure, which can be called exception, signal, interruption, trap or something else in the jargon of the CPU manufacturer. None of them - even if the term "exception" is used - has anything to do with C ++ language exceptions.

In C ++, since it is usually expensive in CPU cycles and object dimensional code to retest for division by zero, the compiler-generated code for the built-in types is not required for any such verification. In practice, it is usually sufficient to trust what the programmer will code to avoid dividing by zero by inserting explicit checks into the subset of sections where they are useful; factoring such checks to avoid redundancy.

If a programmer wants a consistent, guaranteed check, he can create a custom type (a class with user-overloaded operators) that can be used instead of the built-in numeric type, but takes time to check for division by zero (or overflow, overflow or any other problems that the developer has ) and reacts, however, the programmer likes. I understand that in languages ​​like JAVA and C # there is no operator overloading, which, I think, means they cannot painlessly replace the built-in type in this way, requiring invasive code changes to explicitly call functions instead of using intuitive mathematical operators.

In any case, since the C ++ standard itself does not indicate any behavior for situations with delimiters to zero, the implementation may provide some potentially useful behavior if it chooses. This might imagine that somehow generating a real C ++ language exception, but in practice it might be too expensive for processor cycles and code size to justify. Perhaps JAVA is so slow and bloated that a little extra check like this, neither here nor there ...?; -)

Suppose you are on an x86 family processor, the term for a 0 division notification is “interrupt”. But, if UNIX or Linux is running on this machine, partitioning results in a “signal” at the operating system level, and you can set up a signal handler to receive notification of a problem.

+2
source

You write

I understand that all exceptions in C ++ ultimately extend exception

It is not right. C ++ exception can be of any type. With C ++ 98, it should have been copied, but perhaps (and most likely) this restriction was removed with C ++ 11.

In the Java world, catching Exception e would work regardless of the type of Exception. How is this done in C ++?

The catch-all application

catch( ... )

The main problem is that if you want to get any information about the exception, then in C ++ 98 you need to reconstruct, which is not particularly effective. And C ++ 11 & rsquo; exception handling tools may not yet be supported by your toolchain since 2012.

Why is there no exception in this fragment?

Because there is no C ++ exception. Typically, the compiler simply refuses to compile the constant expression 34/0 . I can’t think of a compiler other than g ++ that would compile it: Did you really compile this code? .

In any case, if someone manages to compile this code, then from the standard C ++ point of view, this simply leads to Undefined Behavior , where nothing and nothing can happen. If you are lucky, you will get a signal , but nothing is guaranteed. However, you can use the platform's functionality to catch such events.

+1
source

Division by zero does not cause an exception in C ++. See here , here and here for an example.

0
source

All Articles