OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2015 Eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 /** \file Exception.h -- Default behavior for catch-all exception handlers. |
| 19 */ |
| 20 |
| 21 template<class SubHandlers> |
| 22 struct CatchAllVoid |
| 23 { |
| 24 static void Handler() |
| 25 { |
| 26 try |
| 27 { |
| 28 std::rethrow_exception(std::current_exception()); |
| 29 } |
| 30 catch (std::system_error& ex) |
| 31 { |
| 32 SubHandlers::SystemError(ex); |
| 33 } |
| 34 catch (std::runtime_error& ex) |
| 35 { |
| 36 SubHandlers::RuntimeError(ex); |
| 37 } |
| 38 catch (std::logic_error& ex) |
| 39 { |
| 40 SubHandlers::LogicError(ex); |
| 41 } |
| 42 catch (std::exception& ex) |
| 43 { |
| 44 SubHandlers::Exception(ex); |
| 45 } |
| 46 catch (...) |
| 47 { |
| 48 SubHandlers::Unknown(); |
| 49 } |
| 50 } |
| 51 }; |
| 52 |
| 53 template<class SubHandlers, class ReturnType=SubHandlers::return_t> |
| 54 struct CatchAllReturn |
| 55 { |
| 56 static ReturnType Handler() |
| 57 { |
| 58 try |
| 59 { |
| 60 std::rethrow_exception(std::current_exception()); |
| 61 // Apparently VS 2012 does not realize that this function always throws. |
| 62 // Unless we have an explicit return statement, we get a spurious warning
C4715 "not all control paths return a value". |
| 63 return ReturnType(); |
| 64 } |
| 65 catch (std::system_error& ex) |
| 66 { |
| 67 return SubHandlers::SystemError(ex); |
| 68 } |
| 69 catch (std::runtime_error& ex) |
| 70 { |
| 71 return SubHandlers::RuntimeError(ex); |
| 72 } |
| 73 catch (std::logic_error& ex) |
| 74 { |
| 75 return SubHandlers::LogicError(ex); |
| 76 } |
| 77 catch (std::exception& ex) |
| 78 { |
| 79 return SubHandlers::Exception(ex); |
| 80 } |
| 81 catch (...) |
| 82 { |
| 83 return SubHandlers::Unknown(); |
| 84 } |
| 85 } |
| 86 }; |
| 87 |
| 88 /* |
| 89 * NullHandlers is used at present to define the default exception behavior. |
| 90 * When we get non-trivial default exception behavior, this class can move into
the unit tests. |
| 91 */ |
| 92 struct NullHandlers |
| 93 { |
| 94 static void Unknown() {} |
| 95 static void Exception(std::exception& ex) {} |
| 96 static void LogicError(std::logic_error& ex) { Exception(ex); } |
| 97 static void RuntimeError(std::runtime_error& ex) { Exception(ex); } |
| 98 static void SystemError(std::system_error& ex) { RuntimeError(ex); } |
| 99 }; |
| 100 |
| 101 void DefaultExceptionBehavior() |
| 102 { |
| 103 CatchAllVoid<NullHandlers>::Handler(); |
| 104 } |
| 105 |
OLD | NEW |