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.
Related
I have written very simple program and expected to crash, and it crashes. Now, I have set terminate function, but it will not be called and program just crashed without calling this function.
I have test program both inside debugger/outside debugger, built in both debug/release mode, but showing same behaviour (at least no calling my terminate function). I am musing VS 2012 and Windows 10.
void func()
{
cout<<"Aah you threw exception"<<endl;
}
int main(int argc, char* argv[])
{
set_terminate(func); //setting terminate function
int *p = NULL;
*p =11;
cout<<*p; //this will throw exception
}
C++ is not Java! A signal (or trap) can be generated by dereferencing a nullptr, but it is not automatically translated in C++ exception. On a POSIX compliant system, you can try to use the signal function to catch a signal as proposed by Throwaway Account 3 Million. On Windows, you can try to use the C structured exception handling.
If you don't, and still generate such a trap, the standard just defines that as an Undefined Behaviour, and it commonly just abort the program immediately, bypassing any set_terminate, atexit or whatever function you planned to be call on a controlled termination of your program.
Use signal to catch segmentation errors:
#include <iostream>
#include <signal.h>
using namespace std;
void func(int signal) {
cerr << "Caught signal " << signal << endl;
}
int main() {
signal(SIGSEGV, func);
void (*p)() = NULL;
p();
}
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.
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.
I have the following code taken from cplusplus.com:
// set_terminate example
#include <iostream>
#include <exception>
#include <cstdlib>
using namespace std;
void myterminate () {
cout << "terminate handler called\n";
abort(); // forces abnormal termination
}
int main (void) {
set_terminate (myterminate);
throw 0; // unhandled exception: calls terminate handler
return 0;
}
As there is unhandled exception in the code, it needs to call myterminate() function which is set as terminate handler and supposed to override the default terminate handler.
The program is crashing but not calling myterminate(). I am using Visual C++ 2008 Express Edition.
What's the issue with the code?
One possibility - if you are running the program inside VC++ debugger, the debugger catches unhandled exceptions and it might not return control back to the running program to run myterminate. Try to run your program outside Visual 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.