How to get the exception reported to boost::future? - c++

If I use Boost futures, and the future reports true to has_exception(), is there any way to retrieve that exception? For example, here is the following code:
int do_something() {
...
throw some_exception();
...
}
...
boost::packaged_task task(do_something);
boost::unique_future<int> fi=task.get_future();
boost::thread thread(boost::move(task));
fi.wait();
if (fi.has_exception()) {
boost::rethrow_exception(?????);
}
...
The question is, what should be put in the place of "?????"?

According to http://groups.google.com/group/boost-list/browse_thread/thread/1340bf8190eec9d9?fwc=2, you need to do this instead:
#include <boost/throw_exception.hpp>
int do_something() {
...
BOOST_THROW_EXCEPTION(some_exception());
...
}
...
try
{
boost::packaged_task task(do_something);
boost::unique_future<int> fi=task.get_future();
boost::thread thread(boost::move(task));
int answer = fi.get();
}
catch(const some_exception&)
{ cout<< "caught some_exception" << endl;}
catch(const std::exception& err)
{/*....*/}
...

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;
};

Setting exeption for promise causes abort call

I have a throwing function:
void calculateValuesThrowing(std::promise<int>&& pr, int a, int b)
{
try
{
auto timeout = std::chrono::seconds(5);
logInfo("Calculating value...");
std::this_thread::sleep_for(timeout);
if (a == b)
{
throw std::runtime_error("a cannot equal b");
}
pr.set_value(a + b);
}
catch(std::exception& exc)
{
pr.set_exception(std::current_exception()); // ok, jump here right after the "throw" above
}
}
and I call it like that:
somewhere in main() function:
try
{
std::promise<int> promise;
auto future = promise.get_future();
std::thread th(calculateValuesThrowing, std::move(promise), 10, 10);
auto result = future.get();
th.join();
}
catch(std::exception& exc)
{
std::cout << "Error:" << exc.what(); // never get there
}
I expect an exception of calculateValuesThrowing will be "rethrown" so that I could process it in main's catch(), but right after calculateValuesThrowing finishes working I get abort().
What am I doing wrong?
When future.get() throws, th.join() is never invoked so th goes out of scope while the thread is still active. This terminates the program.
Try [sic] something like this instead:
std::promise<int> promise;
auto future = promise.get_future();
std::thread th(calculateValuesThrowing, std::move(promise), 10, 10);
try
{
auto result = future.get();
}
catch(std::exception& exc)
{
std::cout << "Error:" << exc.what(); // never get there
}
th.join();
Of course, now you can't use result outside of the try, but that was the case anyway. I don't have enough information about how you use it to suggest a concrete fix for that.

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

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(...));
...