Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

exception

It is my understanding that all exceptions in c++ ultimately extend exception. In Java world, catching Exception e would have worked regardless of the type of Exception. How is this done in C++?

Why is that in this snippet exception is not caught?

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 can one find out what actions causes what exception?

like image 992
James Leonard Avatar asked Aug 29 '12 03:08

James Leonard


People also ask

Can you catch exceptions in C?

C itself doesn't support exceptions but you can simulate them to a degree with setjmp and longjmp calls.

Which statement is used to catch all type of exception?

The try statement identifies a block of statements within which an exception might be thrown. The catch statement must be associated with a try statement and identifies a block of statements that can handle a particular type of exception.

How do you handle all exceptions in C++?

CPP. 2) There is a special catch block called the 'catch all' block, written as catch(…), that can be used to catch all types of exceptions. For example, in the following program, an int is thrown as an exception, but there is no catch block for int, so the catch(…) block will be executed.

Can we catch exception?

Yes, we can catch an error. The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the throw statement.


2 Answers

Why is that in this snippet exception is not caught?

An integer divided by 0 is not a standard c++ exception. So no exception is thrown in this case what you get is an plain Undefined Behavior.

Some specific compilers might map this scenario to an particular exception and you will have to check your compiler documentation to find the same.However, using such a feature will be non-portable and your code will be restricted to your specific compiler.

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

Also, in C++, how can one find out what actions causes what exception?

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

like image 151
Alok Save Avatar answered Oct 20 '22 16:10

Alok Save


Divide by 0 causes most CPUs to follow some kind of escalation procedure which may be called an exception, signal, interrupt, trap or whatever in that CPU manufacturer's jargon. None of these - even if the term "exception" is also used - have any direct relationship to C++ language exceptions.

In C++, because it's generally expensive in CPU cycles and object code size to test repeatedly for divide by zero, compiler generated code for the inbuilt types is not required to do any such checking. In practice, it's usually sufficient to trust that the programmer will code to avoid a divide by zero, inserting explicit checks in the subset of divisions where they're useful; factoring such checks to avoid redundancy.

If the programmer wants a consistent guaranteed check, they can create a User Defined Type (a class with custom overloaded operators) that can be used in place of an inbuilt numeric type, but takes the time to check for division by zero (or underflow, overflow or whatever other concerns the developer has) and reacts however the programmer likes. I gather that languages like JAVA and C# lack operator overloading, which I'd imagine means they can't painlessly replace an inbuilt type in this way, needing invasive code changes to explicitly call functions instead of using the intuitive mathematical operators.

Anyway, as the C++ Standard itself doesn't specify some behaviour for divide-by-zero situations, the implementation's free to provide some potentially useful behaviour if it chooses. That could imaginably include somehow generating an actual C++ language exception, but in practice it's likely to be too expensive in CPU cycles and code size to justify. Perhaps JAVA is so slow and bloated anyway that a little extra checking like this is neither here nor there...? ;-)

Say you're on a x86-family CPU, the term for a divide by 0 notification is "interrupt". But, if that machine's running say UNIX or Linux, the division results in a "signal" at the Operating System level, and you can set a signal handler to get notification of the problem.

like image 2
Tony Delroy Avatar answered Oct 20 '22 17:10

Tony Delroy