Question about Exceptions - c++

I was just playing around with exceptions in the visual studio and with the above code I was expecting that since my exception specification doesn't mention anything the bad_exception should have been thrown. But what actually happens is the exception gets caught by the appropriate handler. Why so? Am i missing some setting or something in the IDE?
While i got stuck at above mentioned, Actually I was trying to find answer to the question,If i have a exception blank specification then what gets called?
the unexpected() method or a *bad_exception* will be thrown and if both in what order?
Here's the code.
#include "stdafx.h"
#include <stdio.h>
#include <exception>
#include <iostream>
using namespace std;
class A
{
public:
int i;
};
void myunexpected ()
{
cerr << "unexpected called\n";
}
void doSomething(void) throw();
void doSomething(void) throw()
{
A obj;
obj.i= 100;
throw obj;
}
int _tmain(int argc, _TCHAR* argv[])
{
set_unexpected (myunexpected);
try
{
doSomething();
}
catch (bad_exception be)
{
puts("Caught something");
}
catch (A &obj)
{
puts("Caught Integer");
}
return 0;
}

Regarding exception specification, Visual Studio is not standard-conforming.
While the empty exception specification is somewhat useful (but, as said, not properly implemented by VS), in general exception specifications are seen as an experiment that failed.

Basically, exception specifications are almost useless and in many compilers implemented different to what the standard states. Look at your compiler documentation for more information.
http://msdn.microsoft.com/en-us/library/wfa0edys(VS.80).aspx
I can imagine that this means in particular that the VS compiler will use the exception specification to avoid generating code required for stack unwinding and that in the event of an exception actually being thrown you will end up with undefined behavior.

Related

Why does rethrowing an exception discards the information given by 'what()'?

I'm using MinGW gcc (or g++) 7.1.0 on Windows 10.
Normally, throwing an std::runtime_error shows information like this:
terminate called after throwing an instance of 'std::runtime_error'
what(): MESSAGE
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
But the following code only shows the last two lines, and the what() information is lost:
#include <stdexcept>
using namespace std;
int main() {
try {
throw runtime_error("MESSAGE");
} catch (...) {
throw;
}
}
So the code above only outputs:
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
The same thing happens if I replace ... with const exception&, const runtime_error& (or without const, without &, or without both).
As I know, throw; rethrows the current caught exception. So why isn't what() shown?
What makes you think that rethrowing exception discards the information given by 'what()'? You never inspect what what() returns after rethrowing. This application has requested... message is shown because you uncaught exception caused program to be terminated. what() content is not supposed to be printed automatically.
You can print value return by what() without any problem:
#include <stdexcept>
#include <iostream>
int main()
{
try
{
try
{
throw ::std::runtime_error("MESSAGE");
}
catch (...)
{
throw;
}
}
catch(::std::exception const & exception)
{
::std::cout << exception.what() << ::std::endl;
}
}

C++ exception not being handled in thread

Why is no unhandled exception exception given by VS 2013, or any abort signal raised when the following code is executed?
#include <thread>
void f1()
{
throw(1);
}
int main(int argc, char* argv[])
{
std::thread(f1);
}
The C++ standard states that std::terminate should be called in the following situation:
when the exception handling mechanism cannot find a handler for a thrown exception (15.5.1)
in such cases, std::terminate() is called (15.5.2)
The problem is that in this code, main() could end before the spawned thread (f1).
Try this instead:
#include <thread>
void f1()
{
throw(1);
}
int main(int argc, char* argv[])
{
std::thread t(f1);
t.join(); // wait for the thread to terminate
}
This call terminate() on Coliru (gcc).
Unfortunately Visual Studio 2013 will call directly abort() instead of terminate() (in my tests at least) when encountering this so even adding a handler (using std::set_handler() ) will apparently not work.
I reported this to the VS team.
Still, this code will trigger an error, while your initial code is not garanteed to.

"set_unexpected" doesn't work in VC2010?

I am using VC2010, and write following code to test "set_unexpected" function.
#include <iostream>
#include <exception>
void my_unexpected_handler()
{
std::cout << "unexpected handler" << std::endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
set_unexpected(my_unexpected_handler);
throw 1;
return 0;
}
However, "my_unexpected_handler" is never called(the string isn't printed to console, I tried to set breakpoint in my_unexpected_handler, didn't run into).
What's wrong with my code?
Thanks
Sorry, I misunderstood the unexpected exception. However, even if I change code to following
#include <iostream>
#include <exception>
void my_unexpected_handler()
{
std::cout << "unexpected handler" << std::endl;
}
void func() throw(int)
{
throw 'h';
}
int _tmain(int argc, _TCHAR* argv[])
{
std::set_unexpected(my_unexpected_handler);
func();
return 0;
}
It still doesn't work? That is, "my_unexpected_handler" isn't called.
You are likely doing nothing wrong. You are using visual studio 2010 and that compiler does not support exception specifications. Well, it will syntax check code using them, but will not check the exceptiontype at runtime.
see http://msdn.microsoft.com/en-us/library/wfa0edys(v=vs.100).aspx
This isn't generally considered a problem as exceptions specifications are generally considered not to be useful, and have in fact been deprecated in then c++11 standard
Since the function throwing the exception has no throw specification, the exception is expected. The unexpected exception handler, logically enough, handles only unexpected exceptions. Nothing makes this exception unexpected.

Exception Handling - set_unexpected() not able to call

#include "iostream"
#include "conio.h"
#include "exception"
#include "cstdlib"
using namespace std;
void myunexpected ()
{
cerr << "unexpected called\n";
throw 0; // throws int (in exception-specification)
}
void myfunction () throw (int)
{
throw 'x'; // throws char (not in exception-specification)
}
int main (void)
{
set_unexpected (myunexpected);
try
{
myfunction();
}
catch (int) { cerr << "caught int\n"; }
catch (...) { cerr << "caught other exception (non-compliant compiler?)\n"; }
getch();
return 0;
}
Output(When executed on Visual studio 2008):
caught other exception (non-compliant compiler?)
But, I was expecting the output to be:
unexpected called
caught int
NOTE: I executed this program on Visual Studio 2008.
Yes, as per the Standard the output should be[#1]:
unexpected called
caught int
gcc gives accurate result.
Note that, MSVC is notoriously buggy w.r.t handling exception specifications. Exception specifications are considered a failed experiment.
AFAIK, MSVC does not implement exception specifications, except for the empty ones (throw()/nothrow)
C++03 Standard:
[#1] 15.5.2 The unexpected() function [except.unexpected]
The unexpected() function shall not return, but it can throw (or re-throw) an exception. If it throws a new exception which is allowed by the exception specification which previously was violated, then the search for another handler will continue at the call of the function whose exception specification was violated....

about setting up the set_terminate function

i am using a visual studio c++ compiler,& during my study on exception handling,i came across a number of features that can't be supported by visual c++ compiler,
like controlling the exceptions that can be thrown out of a function.
also i was unable to modify the functioning of terminate() using set_terminate() .
is it a specification too for visual c++ to modify terminate()?...& if so,then can anyone explain that why microsoft is creating these specifications in its compilers?...:-x
what do you mean you were unable to modify terminate
have you tried something like this ?
// set_terminate example
#include <iostream>
#include <exception>
#include <cstdlib>
using namespace std;
void myterminate () {
cerr << "terminate handler called\n";
abort(); // forces abnormal termination
}
int main (void) {
set_terminate (myterminate);
throw 0; // unhandled exception: calls terminate handler
return 0;
}
Don't try to run from VS. Compile and exec from command line.