1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| #include <iostream>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/front.hpp>
#include <boost/mpl/pop_front.hpp>
#include <boost/mpl/empty.hpp>
using namespace boost::mpl;
template<typename v, bool empty>
struct error_handler_impl
{};
template<typename v>
struct error_handler_impl<v, false>
{
static void apply()
{
try {
throw;
} catch(typename front<v>::type const& e) {
} catch(...) {
typedef typename pop_front<v>::type next;
typedef typename empty<next>::type empty;
error_handler_impl<next, empty::value>::apply();
}
}
};
template<typename v>
struct error_handler_impl<v, true>
{
static void apply()
{
throw;
}
};
template<typename v>
void error_handler()
{
typedef typename empty<v>::type empty;
error_handler_impl<v, empty::value>::apply();
}
#include <exception>
struct exception1 : std::exception
{
virtual char const* what() const throw() { return "exception1"; }
};
struct exception2 : std::exception
{
virtual char const* what() const throw() { return "exception2"; }
};
struct exception3 : std::exception
{
virtual char const* what() const throw() { return "exception3"; }
};
void f1() { throw exception1(); }
void f2() { throw exception2(); }
void f3() { throw exception3(); }
void foo() {
std::cout << "foo" << std::endl;
}
int main()
{
typedef vector<exception1, exception2> exception_pool_1;
try {
f1();
} catch(...) {
error_handler<exception_pool_1>(); // Auto re-throw if no match
std::cerr << "Exception silencieuse sur f1" << std::endl;
}
try {
f2();
} catch(...) {
error_handler<exception_pool_1>();
// Do nothing : just ignore
}
try {
try {
f3();
} catch(...) {
error_handler<exception_pool_1>();
std::cerr << "Exception pool 1 on f3" << std::endl;
}
}
catch(std::exception const& e)
{
std::cout << "exception non catchée " << e.what() << std::endl;
}
try {
f2();
} catch(...) {
error_handler<exception_pool_1>();
foo();
}
return 0;
} |
Partager