Cannot catch std::unexpected - c++

I wanted to know what header is required to catch std::unexpected exception. I am currently doing something like this. I would like the breakpoint of std::unexpected catcher to be hit. I am using VS2012
#include <iostream>
#include <exception>
void myfunction () throw (int)
{
throw "ABC";
}
int main()
{
try
{
myfunction();
}
catch(std::exception &f)
{
//breakpoint
}
catch( std::unexpected &f)
{
//breakpoint
}
}
When i try to build this code I get the error
"Error 2 error C2061: syntax error : identifier 'unexpected'
Am i missing a header ?
Update :
I realize now that std::unexpected is a function. So from what I understand is that if myfunction throws anything other than an int then std::unexpected method is called which defaults to terminate. So my question now becomes if I add the exception handler catch( ...) to the above code the breakpoint in catch( ...) is called. Which exception is that ? Does this catch std::unexpected exceptions ?

std::unexpected is called (yes, it is a function) when a function throws an exception whose type isn't listed in the dynamic exception specification. What you could do is use std::set_unexpected to register a handler that will be called.
#include <iostream>
#include <exception>
void myfunction () throw (int)
{
throw "Lol";
}
int main ()
{
std::set_unexpected ([] {std::cerr << "Unexpected stuff!"; throw 0; });
try
{
myfunction();
}
catch (int) { std::cerr << "caught int\n"; }
catch (...) { std::cerr << "caught some other exception type\n"; }
}
Note that both dynamic exception specifications and std::unexpected/std::set_unexpected became deprecated with C++11.
Updated part:
So my question now becomes if I add the exception handler catch(...)
to the above code the breakpoint in catch(...) is called.
That depends on where you put that handler. Putting it into main wont help - std::terminate will still get called. Putting it into myfunction would work but make the exception specification pointless.
Which exception is that ? Does this catch std::unexpected exceptions ?
catch(...) doesn't do magic. It simply matches every exception type, just as a variadic function (with an ellipse in the parameter-clause) can take every argument type*. It isn't different from the other handlers at all when it comes to unexpected exceptions.
* The slight difference is that catch(...) can catch non-POD-types without invoking undefined behavior.

Loopunroller's answer is correct as far as the C++ standard is concerned, but Visual C++'s implementation of dynamic exception specifications is utterly nonconforming - that is, it does not behave in the way specified in the C++ standard at all.
As explained in their documentation, dynamic exception specifications behaves like this in Visual C++:
no specification or throw (...): function can throw anything
throw (<nonempty-list-of-types>): function can throw anything, including things not in the list
throw (): function can't throw.
Microsoft's compiler generates code for a throw() function on the assumption that it doesn't throw. If it ends up throwing, std::unexpected() is not called, the program will simply behave incorrectly.
This is why your catch(...) in main() ended up catching the exception thrown from myfunction (and why unexpected() isn't called). Basically, in Microsoft's world, the throw (int) specification you attached to myfunction is about as meaningful as whitespace.

Related

c++ exception handling

Learning "try & catch". What is wrong with the following code?
Thanks for the advice.
Error in execution:
terminate called without an active exception
Aborted
The code:
#include <stdio.h>
int main()
{
int a = 3;
try
{
if (a < 5)
throw;
}
catch (...)
{
printf ("captured\n");
}
return 0;
}
Your throw; statement tries to rethrow a current exception but there probably isn't one. You need something like
throw some_exception_object();
Inside of a try block, you have to specify what to throw. The only place you can use throw by itself is inside of a catch block to re-throw the current exception. If you call throw by itself without a current exception being active, you will kill your app, as you have already discovered.
Try this:
#include <stdio.h>
int main()
{
int a = 3;
try
{
if (a < 5)
throw 1; // throws an int
}
catch (...)
{
printf ("captured\n");
}
return 0;
}
You can throw anything you want, as long as you throw something.
There are four things, two major and two minor. One thing at a time...
1. Rethrow usage w/o active exception
A throw; statement is used to re-throw an exception that is currently caught. For example:
try {
do_something();
} catch (const std::exception &) {
throw; // This statement re-throws an exception that was caught in this "catch" block.
}
In your case, you are using throw; without catching any exceptions (in order words — it does not appear inside catch block directly or indirectly), thus your program is terminated. When there is a need to throw and not to re-throw an exception, like in your case, you must specify an exception object to be thrown. For example:
throw std::runtime_error("Something bad happened");
2. catch-all clause which does not re-throw a caught exception
Your catch-all clause (catch (...)) is perfectly legal C++. However, it does not re-throw caught exception. Even though it is a legal C++ code, such a usage is a taboo. C and C++ runtime is usually using special types of exceptions to implement certain functionality. For example, NPTL is using exceptions to implement a thread cancellation. If you catch that exception using catch (...), a thread won't be cancelled and you are going to have a bad time. Generally, you have to catch exceptions by their types. In almost all cases, exceptions are inherited from std::exception, and so you have to write catch (const std::exception &) or, if you expect to catch an exact type, - catch(const TypeYouExpect &). If you must, however, use catch-all, remember to re-throw. For example:
try {
do_something();
} catch (...) {
throw; // DO NOT FORGET TO RE-THROW.
}
3. Header naming...
You are including C header whereas C++ provides its own headers for standard C features. So, header:
#include <stdio.h>
.. should be:
#include <cstdio>
C++ specific C functions get special treatment. For example, they become available in std namespace. So that you can use std::open() instead of just open() or ::open(). No big deal, but is highly recommended way to go.
4. Return from main.
Unlike C, C++'s main() function is very special. It allows you not to have return 0;. This is a default behavior. So, unless you really need to return some value, you may save yourself some time by not typing return 0;. Remember, however, that main is the only function like that, and that everywhere else you must explicitly return something unless a function is marked void.
Hope it helps. Good Luck!
You need to actually throw some object. Even something as simple as
throw "error";
will catch the error like you want it to.
see it in action here
The statement to throw an exception is:
throw <expression>;
This statement:
throw;
is also called the re-throw statement and is use to re-throw an existing exception that has been caught. It is typically used in a catch block, for example, you look at the exception and decide if you can continue, retry or abort. In case you decide to abort, you re-throw the exception so that somebody else down the call stack will catch it and handle this error.
For example:
// getResult() calls can fail with a deadlock exception
// This method will retry up to 3 times before failing
Result getResultWithRetry()
{
int nbTry = 3;
for(;;) {
try {
return getResult();
} catch (DeadLockException& e) {
if (nbTry == 0) {
throw; // re-throw the deadlock exception
}
}
--nbTry;
}
}

Exception mechanism issue in c++

I am calling a function and I am throwing an exception in that function. But I don't want to catch that in the same function but want to catch it where that function was called, like here is my example code.
void foo()throw(...){
std::cout << "FOO" <<std::endl;
throw "Found";
}
void main(){
try{
foo();
}
catch(...){
std::cout << "exception catched" <<std::endl;
}
}
But it is crashing at the point where I am throwing the exception in foo function, but I want to catch it in the main function.
How would I do that?
throw;
throw with no operand rethrows the exception that is currently being handled. That means it can only be used in a catch block. Since you aren't in a catch block when the throw; is executed, the program is terminated.
You need to throw something, like a runtime error: throw std::runtime_error("oops");.
Note also that exception specifications (e.g. the throw(...) in void foo() throw(...)) should not be used. For an explanation as to why, see "A Pragmatic Look at Exception Specifications."
Got answer my own question at http://msdn.microsoft.com/en-US/library/wfa0edys%28v=VS.80%29.aspx

Nested try-catch blocks?

int main ()
{
try
{
try
{
throw 5;
}
catch (int n)
{
throw;
}
}
catch (...)
{
cout << "Exception occurred";
}
}
This prints out "Exception occured" but
int main ()
{
try
{
try
{
throw;
}
catch (...)
{
throw;
}
}
catch (...)
{
cout << "Exception occurred";
}
}
This just errors. It seems like I'm doing the try-catch's exactly the same! The only difference is that in the first case I'm throwing an int, then a general exception, but in the second case, I'm throwing a general exception both times. Is the program confused as to which catch to go to?
There's no such thing as "general exception" and you throw no such thing.
In the first example, you throw an int, then you re-throw the exception that you are handling. That's the meaning of throw without an argument.
In the second example you start with an attempt to re-throw an exception that you are handling. As you are not handling an exception at that time, you get an error.
Your 2nd example terminates the program with a good error message (at least using g++ 4.6.1):
terminate called without an active exception
Aborted
This happens because you are trying to re-throw an exception, but since there are no active exception, the program terminates.
The current c++11 draft, in chapter 15.5.1 says this:
In some situations exception handling must be abandoned for less
subtle error handling techniques.
Then in the list of cases when std::terminate is called is this:
when a throw-expression with no operand attempts to rethrow an
exception and no exception is being handled (15.1),
So, the behavior from your 2nd example is well defined in the standard.

Exception handling , unable to understand :(

Hya advance users of SO,
First of all I am new to C++, so pardon me if I don't make myself clear asking question. I've seen a example of exception handling, but couldn't figure out what's happening here :(
http://codepaste.net/zqsrnj
or
enum ErrorCode {…}; // this is exception class
ErrorCode dispatcher() {
try {
throw; // what is thrown here in function?, if rethrow what is rethrown?
}
catch (std::bad_alloc&) {
return ErrorCode_OutOfMemory;
}
catch (std::logic_error&) {
return ErrorCode_LogicError;
}
catch (myownstdexcderivedclass&) {
return ErrorCode_42;
}
catch(...) { // this will handle the above throw in try block of dispatcher
return ErrorCode_UnknownWeWillAllDie;
}
}
ErrorCode apifunc() {
try {
// foo() might throw anything
foo();
}
catch(...) {
// dispatcher rethrows the exception and does fine-grained handling
return dispatcher();
}
return ErrorCode_Fine; //
}
ErrorCode apifunc2() {
try {
// bar() might throw anything
bar();
}
catch(...) {
return dispatcher();
}
return ErrorCode_Fine;
}
Can anyone explain this line by line or overall what's happening here, how control is flowing? Any help is very much appreciated, so thanks a lot.
apifunc() and apifunc2() translate exceptions into error codes, using the dispatcher() function.
Basically, what happens is as follows:
apifunc() (and similarly apifunc2()) attempts to call a function foo(). If foo() throws an exception, then the catch block will call dispatcher() to get the error code corresponding to the exception, and then returns that error code. If foo() doesn't throw, apifunc() returns ErrorCode_Fine indicating no error.
dispatcher() works by re-throwing the last exception thrown, i.e. the one foo() threw. dispatcher() then checks which exception was thrown using the catch blocks, and returns the correct error code. For example, if foo() threw std::bad_alloc, then that catch-block will be executed and return ErrorCode_OutOfMemory;.
Why would someone do this?
Exceptions are not necessarily binary compatible across different compilations (compilers, compiler flags, and so on), so translating exceptions to error codes is more portable across module boundaries.
When foo() throws an exception during its execution, the exception is caught in the apifunc() wrapper whose catch clause invoke the dispatcher() method. There the "current" exception is rethrown (that's the empty throw statement in the dispatcher() method) and caught again. Then the different catch clauses (bad_alloc, logic_error, myownstdexcderivedclass ... returns a sepcific error code than will be returned to the outside world.
The last catch(...) clause ensures that no exception will ever be thrown to the callers of apifunc().

What does a single "throw;" statement do?

These days, I have been reading a lot the C++ F.A.Q and especially this page.
Reading through the section I discovered a "technique" that the author calls "exception dispatcher" that allows someone to group all his exception handling in one handy function:
void handleException()
{
try {
throw; // ?!
}
catch (MyException& e) {
//...code to handle MyException...
}
catch (YourException& e) {
//...code to handle YourException...
}
}
void f()
{
try {
//...something that might throw...
}
catch (...) {
handleException();
}
}
What bothers me is the single throw; statement: if you consider the given example then sure, it is obvious what it does: it rethrows the exception first caught in f() and deals with it again.
But what if I call handleException() on its own, directly, without doing it from a catch() clause ? Is there any specified behavior ?
Additionally for bonus points, is there any other "weird" (probably not the good word) use of throw that you know of ?
Thank you.
If you do a throw; on its own, and there isn't a current exception for it to rethrow, then the program ends abruptly. (More specifically, terminate() is called.)
Note that throw; is the only safe way to re-throw the current exception - it's not equivalent to
catch (exception const & e) { throw e; }
Yes, it specified behavior, it will call terminate;
15.1, para 8: If no exception is presently being handled, executing a
throw expression with no operand calls
terminate() (15.5.1).
That's so-called exception handler. It rethrows the "current exception" if any. If there's no exception currently being handled terminate() will be called.