Error in exception handling with LLVM - c++

I am trying to compile C++ code with CLANG++ as front end and backend as LLVM.
The version is 3.0.
There seems to be a problem with exception handling. Whenever the code throws an exception, the program just terminates with message that "Termination after throwing an exception".
Here is one of the sample code I tried with CLANG ++ .
struct A {};
struct B : virtual A {};
struct C : virtual A {};
struct D : virtual A {};
struct E : private B, public C, private D {};
extern "C" void abort ();
void fne (E *e)
{
throw e;
}
void check(E *e)
{
int caught;
caught = 0;
try { fne(e); }
catch(A *p) { caught = 1; if (p != e) abort();}
catch(...) { abort(); }
if (!caught) abort();
caught = 0;
try { fne(e); }
catch(B *p) { abort ();}
catch(...) { caught = 1; }
if (!caught) abort();
caught = 0;
try { fne(e); }
catch(C *p) { caught = 1; if (p != e) abort();}
catch(...) { abort(); }
if (!caught) abort();
caught = 0;
try { fne(e); }
catch(D *p) { abort ();}
catch(...) { caught = 1; }
if (!caught) abort();
return;
}
int main ()
{
E e;
check (&e);
check ((E *)0);
return 0;
}
I am quite new to LLVM so do not have much idea about it. Also does it have anything related to Exception Handling table generation by LLVM.
The above problem continues for any code.
I have compiled the above code on Linux machine.
Also I tried putting printf on every catch clause but no response. So it seems that when exception was thrown , no matching catch was found for the exception and it led to call of terminate funciton

Seeing your other question... If you're on arm/linux - then such result is expected. The support for EH is not finished there, so, it might be arbitrary broken.

Related

Receive async exception directly

class ClassA
{
void running()
{
int count = 0;
m_worker_stop.store(true);
while (m_worker_stop.load() == false)
{
count++;
if (count == 10)
{
// Make exception
std::vector v(100000000000);
}
}
}
void start()
{
m_worker = std::async(std::launch::async, &ClassA::running, this);
}
void stop()
{
m_worker_stop.store(true);
if (m_worker.valid())
m_worker.get(); // catch exception in this point
}
std::future<void> m_worker;
std::atomic_bool m_worker_stop = { false };
}
class Main // this is single-ton Main class
{
...
void running()
{
try {
m_classA->start();
// Wait for external signal(ex. SIGINT, SIGTERM, ..)
while (true) { // signal check }
m_classA->stop();
}
catch(std::exception& e) {
// re-create throwed object
}
catch(...) {
// re-create throwed object
}
}
}
int main()
{
Manager::getInstance()::running();
return 0;
}
Hello, everyone.
The approximate structure of the program is as above.
In fact, I have not only classA but also many other objects such as B, C, and D.
(start() and stop() function is simillar !)
An exception was raised using std::vector v(1000000..)
However, it became a catch when stop() was activated.
What I actually want is to delete the classA object and re-create it if an exception occurs.
So I need to catch directly when exception was occured.
In this case, is any idea to get exception without wait for signals?
Here is one way of achieving the effect you want:
class Main // this is single-ton Main class
{
...
void running()
{
for (size_t i = 0; i < max_tries; ++i)
{
try {
m_classA->start();
// Wait for external signal(ex. SIGINT, SIGTERM, ..)
while (true) {
// signal check ...
}
m_classA->stop();
// path to happy ending :)
LOG("Main::running(): Operation successful.",
return;
}
catch(std::exception& e) {
LOG("Main::running(): Exception caught: message:\"{}\"", e.what());
}
catch(...) {
LOG("Main::running(): Unspecified exception caught, aborting.");
return; // Example of 'unrecoverable error'
}
// this part is only executed after an exception.
m_classA->shut_down(); // if you need some special shut down after an error.
m_classA.clear(); // this is redundant, but explicit (optional)
m_classA = MakeMeAnA(); // call our favorite A construction method.
}
// path to total failure :(
LOG("Main::running(): Exiting after {} failed attempts", max_tries);
}
private:
static constexpr size_t max_tries = 3;
};

Under what circumstances does EXCEPTION_RECORD link to another nested exception?

The documentation for _EXCEPTION_RECORD says about one of it's members, struct _EXCEPTION_RECORD *ExceptionRecord
A pointer to an associated EXCEPTION_RECORD structure. Exception records can be chained together to provide additional information when nested exceptions occur.
However, I haven't been able to provoke such a situation of nested structured exceptions. Here is what I have tried so far:
#include <iostream>
#include <windows.h>
void Handle0(LPEXCEPTION_POINTERS pex) {
std::cout << "chain0 = " << pex->ExceptionRecord->ExceptionRecord << std::endl;
}
void Handle1(LPEXCEPTION_POINTERS pex) {
std::cout << "chain1 = " << pex->ExceptionRecord->ExceptionRecord << std::endl;
__try {
throw 3;
} __except( Handle0(GetExceptionInformation()), EXCEPTION_EXECUTE_HANDLER ) {}
}
int main() {
__try {
throw 3;
} __except( Handle1(GetExceptionInformation()), EXCEPTION_EXECUTE_HANDLER ) {}
return 0;
}
The pex->ExceptionRecord->ExceptionRecord is always nullptr. Under what circumstances do I get a link to a nested _EXCEPTION_RECORD there?
According to MSDN:
When an exception is raised during the processing of an exception
filter within ... native code ... a nested exception is raised, the
ExceptionRecord field in the EXCEPTION_RECORD structure (as returned
by GetExceptionInformation) is set, and the ExceptionFlags field sets
the 0x10 bit. The following example illustrates this difference in
behavior:
#include <windows.h>
#include <stdio.h>
#include <assert.h>
#ifndef false
#define false 0
#endif
int *p;
int filter(PEXCEPTION_POINTERS ExceptionPointers) {
PEXCEPTION_RECORD ExceptionRecord =
ExceptionPointers->ExceptionRecord;
if ((ExceptionRecord->ExceptionFlags & 0x10) == 0) {
// not a nested exception, throw one
*p = 0; // throw another AV
}
else {
printf("Caught a nested exception\n");
return 1;
}
assert(false);
return 0;
}
void f(void) {
__try {
*p = 0; // throw an AV
}
__except(filter(GetExceptionInformation())) {
printf_s("We should execute this handler if "
"compiled to native\n");
}
}
int main() {
__try {
f();
}
__except(1) {
printf_s("The handler in main caught the "
"exception\n");
}
}
I believe it is also set if you try to continue non-continuable exception. In this case EXCEPTION_RECORD will represent EXCEPTION_NONCONTINUABLE_EXCEPTION, while its ExceptionRecord will point to original exception.

user defined exception handling

This is a c++ program to calculate average, and grade using 5 marks.
If marks entered are greater than 100 or less than 0, student exception should be thrown.
#include<iostream>
#include<exception>
using namespace std;
class lessex:public exception
{
public:
void what()
{
cout<<"Mark less than 0"<<endl;
}
};
class morex:public exception
{
public:
void what()
{
cout<<"Mark greater than 100"<<endl;
}
};
class student
{
string name;
string rollno;
int marks[5];
double avg;
char g;
public:
void get();
void aveg();
void grade();
void print();
};
void student::get()
{
cin>>name;
cin>>rollno;
for(int i=0;i<5;i++)
{
try{
cin>>marks[i];
if(marks[i]>100)
{
morex d;
throw d;
}
}
catch(morex &e)
{
/*e.what();*/
throw ;
}
try{
if(marks[i]<0)
{
lessex d;
throw d;
}
}
catch(lessex &e)
{
/*e.what();*/
throw ;
}
}
}
void student::aveg()
{
int sum=0;
for(int i=0;i<5;i++)
{
sum=sum+marks[i];
}
avg=sum/5;
}
void student::grade()
{
if(avg>90)
g='S';
else
g='Z';
}
void student::print()
{
cout<<name<<endl;
cout<<rollno<<endl;
cout<<g<<endl;
}
int main()
{
student s;morex e;lessex e1;
try{
s.get();
}
catch(morex &e)
{
e.what();
}
catch(lessex &e1)
{
e1.what();
}
s.aveg();
s.grade();
s.print();
return 0;
}
However, my program does not successfully exit after encountering exception in the main function.
Why is it continuing with s.aveg,grade,etc.
Why is my program not exiting after encountering exception-from main function? Why is it continuing with s.aveg,grade,etc.
You catch the exception, and then leave the catch block. Execution continues normally after that. Exceptions aren't re-thrown automatically at the end of a handler's block. That would maddening, what's the point of catching if you can't handle the error and continue running?
If you want the exception re-thrown, you need to add an explicit throw; in the handler. Like you already do in student::get(). Or just not have a try-catch block there. The program will terminate without "s.aveg,grade,etc." being executed.
Or, assuming you intent is not terminate, but to exit gracefully without executing other functions, you can do as user4581301 suggested. Move those function calls into the try block. That way, if an exception is thrown before their execution, they will not run before or after the handler.
You continue execution, without exiting, after catching the exception, and that's why the program isn't exiting.
First, you should follow the convention that what returns a string and doesn't print anything:
class lessex : public exception
{
public:
const char* what() const noexcept override
{
return "Mark less than 0";
}
};
class morex : public exception
{
public:
const char* what() const noexcept override
{
return "Mark greater than 100";
}
};
Then, you're overcomplicating things rather a lot in get;
void student::get()
{
cin >> name;
cin >> rollno;
for(int i = 0; i < 5; i++)
{
cin >> marks[i];
if (marks[i]>100)
{
throw morex();
}
if(marks[i]<0)
{
throw lessex();
}
}
}
and exceptions shouldn't be used like error codes and caught after each potentially throwing call, you normally write the "happy path" (the assumed-to-be-error-free path) and handle exceptions outside it:
int main()
{
try
{
// This part handles the normal case, assuming that all goes well.
student.s;
s.get();
s.aveg();
s.grade();
s.print();
}
// This part handles the exceptional case when something goes wrong.
catch (std::exception& ex)
{
std::cerr << ex.what();
}
}
(Your design is somewhat questionable since throwing from get can leave the object in an invalid state. You might want to rethink it.)
It is continuing because after you catch the exception you don't do anything about it. You have to specify what you would do inside the catch block when the specific exception is thrown.
Alternatively you could also move the other functions like s.aveg(); s.grade(); s.print(); inside try{
s.get();
}
This will prevent the aveg, grade and print functions from stop executing once an exeption is hit

C++Builder does not call destructor on throw-catch-continue

For code:
#include <stdio.h>
int ConstrCalls=0, DestrCalls=0;
struct Z
{
Z() { ConstrCalls++; }
~Z() { DestrCalls++; }
};
int main(int argc, char**)
{
bool Startup= true;
do
{
Z z;
try
{
if( Startup )
{
Startup= false;
throw( 1 );
}
else
{
break;
}
}
catch(int)
{
continue;
}
}
while(true);
printf( "ConstrCalls=%d DestrCalls=%d", ConstrCalls, DestrCalls);
return 0;
}
g++ output is "ConstrCalls=2 DestrCalls=2", ok
Embarcadero C++Builder 2010, C++Builder 10 Seattle output is "ConstrCalls=2 DestrCalls=1", that is after throw-catch-continue destructor was not called!
Can C++Builder works right?
Thanks.
Sadly...
the C++Builder 6, RAD Studio 2009 and XE7 Update 1, C++ compiler generates bad exception handling code (and in all likelihood all compilers in between--those are the compilers I current have access to). When an exception is thrown, the stack unwind code has been observed to:
Crash with an access violation
Fail to execute destructors when it should
Leak memory
Fire destructors twice
This makes it impossible to produce reliable, exception-safe, C++ software with this compiler.
Take a look at C++ compiler exception handling is completely broken for further details.
This is from the section "Not Firing Destructors When It Should":
The following Button OnClick event handler constructs temporary Count objects only within the try/catch scope. Count objects keep track of the number of live instances in the count variable.
As an invariant the number of Count instances should be 0 following the try/catch. Instead this fails with BAD STACK UNWIND.
static int count = 0;
struct Count {
Count() { ++count; }
Count(const Count& rhs) { ++count; }
~Count( ) { --count; }
};
static int __fastcall destructorNotFired(int i=0, Count c=Count() ) {
throw Exception( "DESTRUCTOR NOT FIRED" );
}
void __fastcall TExceptionBugTestForm::ButtonDestructorNotFiredClick(TObject *Sender)
{
assert(count == 0);
try {
destructorNotFired( destructorNotFired() );
} catch ( Exception& e ) {
}
if (count != 0) {
ShowMessage( "BAD STACK UNWIND" );
}
assert(count == 0);
}
(very similar to your code).
Exception handling works well compiling code with clang++ (only 64bit for C++Builder != XE10).

Error checking on many function calls

Sometimes when I am programming in C++/C I end up calling the same function multiple times and I was wondering what is the most efficient way to check for errors for all of those calls? Using if else statements take up a lot of code and look ugly. I have come up with my own way of checking for errors, perhaps there is a better way that I should use.
int errs[5] = {0};
errs[0] = functiona(...);
errs[1] = functiona(...);
...
errs[5] = functiona(...);
for (int i = 0; i < 5; i++)
{
if (err[i] == 0)
MAYDAY!_wehaveanerror();
}
Note: I understand that using try and catch might be better for C++ as it would solve this problem by throwing an exception on the first error, but the problem with that is that it is not compatible with a lot of functions that return error codes such as the Windows API. Thanks!
You could write some pseudo-C++ like this:
struct my_exception : public std::exception {
my_exception(int); /* ... */ };
int main()
{
try
{
int e;
if ((e = function()) != SUCCESS) { throw my_exception(e); }
if ((e = function()) != SUCCESS) { throw my_exception(e); }
if ((e = function()) != SUCCESS) { throw my_exception(e); }
}
catch (my_exception & e)
{
std::cerr << "Something went wrong: " << e.what() << "\n";
}
}
If...IF the function has a chance to throw a different error you should also add a catch all.
struct my_exception : public std::exception {
my_exception(int); /* ... */ };
int main()
{
try
{
int e;
if ((e = function()) != SUCCESS) { throw my_exception(e); }
if ((e = function()) != SUCCESS) { throw my_exception(e); }
if ((e = function()) != SUCCESS) { throw my_exception(e); }
}
catch (my_exception & e)
{
std::cerr << "Something went wrong: " << e.what() << "\n";
}
catch (...)
{
//Error Checking
}
}
What about handling the checking in a function?
void my_function() {
if (!create_window())
throw Error("Failed to create window");
}
int main() {
try {
my_function();
} catch (const Error& e) {
cout << e.msg << endl;
} catch (...) {
cout << "Unknown exception caught\n"
}
return 0;
}
If you're calling the same function over and over again, the most succinct way might be to use a macro. I would suggest something like:
#define CHECKERROR(x) if(x == 0) wehaveanerror()
CHECKERROR(function(...));
CHECKERROR(function(...));
Obviously, this macro would be very specific to the particular function and error handler involved, so it may be prudent to undef it after those calls.
Doing it more old-school, but keeping w/ the original error response but responding as soon as an error occurs w/o looking ugly:
#define callcheck(r) if ((r)==0) MAYDAY!_wehaveanerror()
callcheck(functiona(...));
callcheck(functiona(...));
...