I am learning about exceptions from a book and try/catch and the OS should terminate the following program.
The book says, the message terminate called after throwing an instance of 'std::bad_alloc' should show up. But doesn't.
I am using Arch Linux and the program is not stopping. It runs, fills the RAM a bit linear until it doesn't (at about 90%), the processor is working a lot but no freezing and no terminating.
Is this a Windows only use case or how could I reproduce the error on a Linux/maybe Unix system?
#include <iostream>
#include <exception> //c++ exception
int main()
{
int *feld;
int loop = 1;
for(;;) //infinite loop
{
std::cout << "Loop number: " << loop << '\n';
try
{
feld = new int[10000];
loop++;
if (durchlauf == 100000) //since c++11
std::terminate();
}
catch(...)
{
std::cout << "Error, Program done.\n";
break;
}
}
return 0;
}
EDIT: I found out that my OOM killer is not working properly with swap enabled/at all. But c++ has its own termination process call
https://en.cppreference.com/w/cpp/error/terminate
It just doesn't issues an exception to print out the catch line.
Has anyone a hint to issue a catch termination?
I found the following code for you to program some terminations:
Hope that helps.
#include <iostream>
#include <stdexcept>
struct A {
int n;
A(int n = 0): n(n) { std::cout << "A(" << n << ") constructed successfully\n"; }
~A() { std::cout << "A(" << n << ") destroyed\n"; }
};
int foo()
{
throw std::runtime_error("error");
}
struct B {
A a1, a2, a3;
B() try : a1(1), a2(foo()), a3(3) {
std::cout << "B constructed successfully\n";
} catch(...) {
std::cout << "B::B() exiting with exception\n";
}
~B() { std::cout << "B destroyed\n"; }
};
struct C : A, B {
C() try {
std::cout << "C::C() completed successfully\n";
} catch(...) {
std::cout << "C::C() exiting with exception\n";
}
~C() { std::cout << "C destroyed\n"; }
};
int main () try
{
// creates the A base subobject
// creates the a1 member of B
// fails to create the a2 member of B
// unwinding destroys the a1 member of B
// unwinding destroys the A base subobject
C c;
} catch (const std::exception& e) {
std::cout << "main() failed to create C with: " << e.what();
}
Just for the sake of being helpful if someone steps into the same problem
a coded thrown exception after 100000 loops:
#include <iostream>
#include <exception> //c++ exception
int main()
{
int *feld;
int loop = 1;
for(;;) //infinite loop
{
std::cout << "Loop number: " << loop << '\n';
try
{
feld = new int[10000];
loop++;
if (loop == 1e5)
throw std::bad_alloc(); //has to be inside the try(){} scope
}
catch(...)
{
std::cout << "Error, Program done.\n";
break;
}
}
return 0;
}
Related
#include <iostream>
#include <any>
using namespace std;
class c {
public:
c() :a{ 0 } { cout << "constructor\n"; }
c(int aa) :a{ aa } { cout << "Constructor\n"; }
~c() { cout << "destructor\n"; }
int get() { return a; }
private:
int a;
};
auto main()->int
{
any a{ 5 };
cout << any_cast<int>(a) << '\n';
a.emplace<c>(3);
cout << '!' << any_cast<c>(a).get() << '\n';
//des
cout << '\n';
a.emplace<c>(9);
cout << '!' << any_cast<c>(a).get() << '\n';
//des
}
destructor called after each any_cast.
and, below code makes run-time error.
I think the cause is any_cast(C)'s work pipeline is might be like
~C() then X(C) ERROR!!C doesn't exist
any_cast really work like that?
I add blow codes and make run-time error.
class X {
public:
X() :a{ 0 } { cout << "xonstructor\n"; }
X(c& aa) :a{ aa.get() } { cout << "Xonstructor\n"; }
~X() { cout << "Xdestructor\n"; }
int get() { return a; }
private:
int a;
};
auto main()->int
{
any a{ 5 };
cout << any_cast<int>(a) << '\n';
a.emplace<c>(3);
cout << '!' << any_cast<X>(a).get() << '\n';
//runtime error after '!'
cout << '\n';
a.emplace<c>(9);
cout << '!' << any_cast<X>(a).get() << '\n';
}
You are copying a c (or X) from the one within the std::any. That copy is destroyed at the end of the expression, after having been streamed out.
any_cast does not do any conversion. It throws if you ask it for a type different to the one it stores. When you have emplaced a c and asked for an X, it throws std::bad_any_cast, because X is not c.
boost::thread interrupt() throws an exception only the first time that an interruption point is executed? My case is the following.
I create a boost::thread which executes functionA. functionA calls functionB and when on functionB we call function threadInterrupt. functionB throws a boost::thread_interrupted exception and returns to functionA. My question is if a new boost::thread_interrupted exception will be throw in functionA when another interruption point is executed.
void MyClass::function(int arg1, int arg2) try {
boost::thread* t = new boost::thread(boost::bind(&MyClass::functionA, this, var1, var2));
} catch (...) {
cout << "function throw an exception" << endl;
}
void MyClass::functionA(int arg1, int arg2 ) try {
//some code here
try {
int retVal = MyClass::functionB(var);
boost::this_thread::interruption_point();
//some code here
} catch(boost::thread_interrupted&) {
cout << "thread interrupted in functionA" << endl;
}
} catch (...) {
cout << "functionA throw an exception" << endl;
}
int MyClass::functionB(int arg1) try {
//some code here
try {
boost::this_thread::interruption_point();
//some code here
} catch(boost::thread_interrupted&) {
cout << "thread interrupted in functionB" << endl;
return 0;
}
return 1;
} catch (...) {
cout << "functionB throw an exception" << endl;
return 1;
}
void MyClass::threadInterrupt() {
if (thr!=NULL) {
thr->interrupt();
thr->join();
delete thr;
thr=NULL;
}
}
Have you tried it? See it Live On Coliru
#include <boost/thread.hpp>
#include <iostream>
using namespace boost;
int main() {
thread t([]{
int i = 0;
while (true) try {
if (i >= 10) return;
while (i < 10) {
this_thread::sleep_for(chrono::milliseconds(200));
std::cout << "processed " << i++ << "\n";
}
}
catch (...) { std::cout << "Interrupted at i = " << i << "\n"; }
});
this_thread::sleep_for(chrono::milliseconds(800));
t.interrupt();
t.join();
std::cout << "Interrupt requested? : " << std::boolalpha << t.interruption_requested() << "\n";
}
Output:
processed 0
processed 1
processed 2
Interrupted at i = 3
processed 3
processed 4
processed 5
processed 6
processed 7
processed 8
processed 9
Interrupt requested? : false
As you can see the interrupt request behaves like a sort of auto-reset flag.
Code sample one:
try {
exception e;
throw e;
} catch(exception& refer)
Code sample two:
exception& method()
{
exception e;
return e;
}
Some books mentioned that code sample one is ok, code two is wrong due to e in it is a local variable and will destroy when the function ends, but my question is why is sample code one okay? Isn't the e a local variable too?
Yes, in sample one, e is local to the try block, and is destroyed upon exiting that scope. But when you throw, the compiler makes a copy (or move), who's lifetime is extended to the end of the catch block, and it is this copy to which the reference refer refers.
For figuring out what the compiler does in cases like this, I always like to pull out my Noisy class.
#include <iostream>
class Noisy
{
public:
Noisy()
{ std::cout << "Noisy default construct\n"; }
Noisy(Noisy const&)
{ std::cout << "Noisy copy\n"; }
Noisy(Noisy&&)
{ std::cout << "Noisy move\n"; }
~Noisy()
{ std::cout << "Noisy destroy\n"; }
Noisy& operator=(Noisy const&)
{ std::cout << "Noisy copy assign\n"; return *this; }
Noisy& operator=(Noisy&&)
{ std::cout << "Noisy move assign\n"; return *this; }
void swap(Noisy&)
{ std::cout << "Noisy swap\n"; }
};
int main(int argc, char* argv[])
{
try
{
std::cout << "in try block\n";
Noisy n;
std::cout << "about to throw n\n";
throw n;
std::cout << "end of try block\n";
}
catch (Noisy & n)
{
std::cout << "in catch block\n";
}
std::cout << "after catch\n";
}
I have read several articles here and else where that it is OK to throw exception from constructor. However, I have noticed that it doesn't call destructor of base class or its data members if an exception is thrown from the constructor. Consider the following example:
#include <iostream>
using namespace std;
struct C
{
C() { cout << __FUNCTION__ << endl; }
~C() { cout << __FUNCTION__ << endl; }
};
struct E: public C
{
C c;
E() { cout << __FUNCTION__ << endl; throw 4; }
~E() { cout << __FUNCTION__ << endl; }
};
int main()
{
E e;
}
$ g++ test.cpp; ./a.exe
C
C
E
terminate called after throwing an instance of 'int'
Aborted (core dumped)
In this case, E's constructor throws an exception but C's destructor as a data member or as a base class is not called. Now if C's destructor performs some cleanup operation like closing files/sockets and deleting heap allocations, this can cause problems.
So my question is why and when is it OK to throw exceptions from constructors.
If you catch the error, the destructor will be run. When an uncaught exception is thrown in C++, the runtime calls std::terminate. By default, std::terminate calls std::abort which specifically does not call destructors on the way out.
With this version:
#include <iostream>
using namespace std;
struct C
{
C() { cout << __FUNCTION__ << endl; }
~C() { cout << __FUNCTION__ << endl; }
};
struct E: public C
{
C c;
E() { cout << __FUNCTION__ << endl; throw 4; }
~E() { cout << __FUNCTION__ << endl; }
};
int main()
{
try {
E e;
} catch(...) {
}
return 0;
}
I get output:
C
C
E
~C
~C
I have noticed that it doesn't call destructor of base class or its data members if an exception is thrown from the constructor
Yes, it does.
However, since you don't catch that exception in the entire program, the program is immediately terminated.
If you were to catch the exception somewhere higher up the call stack, then the destructors of base class and members would be invoked as expected.
You don't handle the "exception".
> cat test.cpp
#include <iostream>
using namespace std;
struct C
{
C() { cout << __FUNCTION__ << endl; }
~C() { cout << __FUNCTION__ << endl; }
};
struct E: public C
{
C c;
E() { cout << __FUNCTION__ << endl; throw 4; }
~E() { cout << __FUNCTION__ << endl; }
};
int main()
{
try
{
E e;
}
catch (int i)
{
std::cerr << "Handled " << i << std::endl;
}
}
Build and run..
> make test
make: `test' is up to date.
> ./test
C
C
E
~C
~C
Handled 4
>
Both Cs destructed and a perfectly normal termination.
1) E's constructor catched the exception and ran completly.
Therefore, its object is created and the distructor is
invoked.
struct C
{
C() {cout <<__FUNCTION__<< endl;}
~C() {cout <<__FUNCTION__<< endl;}
};
struct E: public C
{
C c;
E() {
try {
cout <<__FUNCTION__<< endl;
throw 4;
}
catch(int i) {
cerr<<"int "<<i<<" is catched by "<<__FUNCTION__<<endl;
}
}
~E() {cout << __FUNCTION__ << endl;}
void print(){
cout<<"obj of class E is created"<<endl;
}
};
int main()
{
try {
E e;
e.print();
}
catch(int i) {
cerr<<"int "<<i<<" catched by "<<__FUNCTION__<<" function"<<endl;
}
return 0;
}
/*
Results:
C::C
C::C
E::E
int 4 is catched by E::E
obj of class E is created
E::~E
C::~C
C::~C
*/
2) E's constructor didn’t catch the exception and ran incompletly.
In result, its object is not created. Therefore, its distructor
is not invoked.
struct C
{
C() {cout <<__FUNCTION__<< endl;}
~C() {cout <<__FUNCTION__<< endl;}
};
struct E: public C
{
C c;
E() {
try {
cout <<__FUNCTION__<< endl;
throw 4;
}
catch(float i) {
cerr<<"int "<<i<<" is catched by "<<__FUNCTION__<<endl;
}
}
~E() {cout << __FUNCTION__ << endl;}
void print(){
cout<<"obj of class E is created"<<endl;
}
};
int main()
{
try {
E e;
e.print();
}
catch(int i) {
cerr<<"int "<<i<<" catched by "<<__FUNCTION__<<" function"<<endl;
}
return 0;
}
/*
Results:
C::C
C::C
E::E
C::~C
C::~C
int 4 catched by main function
*/
In the following code, what is the order in which the destructors of b, q and e are called, and which is called before handling the exception.
(The "cout..." parts are leftover for the original question)
#include <iostream>
using namespace std;
class A {
public:
A(int arg) : m(arg) {
cout << "A::A(int) " << m << endl;
m = 2*arg;
}
virtual void f() {
cout << "A::f() " << m << endl;
}
void g() {
cout << "A::g(A) " << m << endl;
}
int m;
};
class B : public A {
public:
B(int arg) : A(arg) {
cout << "B::B(int) " << m << endl;
m = 3*arg;
}
~B() {
cout << "B::~B()" << endl;
}
void f() {
cout << "B::f(A&) " << m << endl;
}
virtual void g() {
B q(*this);
throw q;
cout << "B::g(A) " << m << endl;
}
};
int main() {
try {
B b(1);
b.g();
} catch (A e) {
cout << "Error: ";
e.f();
}
return 0;
}
If it's possible, could you explain the reason.
Thank you.
q's desctructor is called first because it is destroyed as the first part of stack unwinding (local objects in the inner most scope are destroyed first), then b's destructor is called, also as part of stack unwinding. Both are destroyed before the catch block is even entered. Stack unwinding happens before the exception handler is executed.
Bear in mind, though, that exception objects are copied so in B::g() a copy (the exception object) of q which is a copy of b is created and its lifetime extends to the end of the catch block. It is used to initialize (slicing!) e.
The destructor for the exception object itself (a copy of q) is executed immediately after e is destroyed (as the handler doesn't exit via a throw;). This ordering is specified in the standard.