Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: src/plugin/Exception.h

Issue 5137721374801920: Issue #1173 - Default behavior for catch-all blocks
Patch Set: Created Feb. 25, 2015, 9:44 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/plugin/Exception.h
===================================================================
new file mode 100644
--- /dev/null
+++ b/src/plugin/Exception.h
@@ -0,0 +1,105 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-2015 Eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/** \file Exception.h -- Default behavior for catch-all exception handlers.
+ */
+
+template<class SubHandlers>
+struct CatchAllVoid
+{
+ static void Handler()
+ {
+ try
+ {
+ std::rethrow_exception(std::current_exception());
+ }
+ catch (std::system_error& ex)
+ {
+ SubHandlers::SystemError(ex);
+ }
+ catch (std::runtime_error& ex)
+ {
+ SubHandlers::RuntimeError(ex);
+ }
+ catch (std::logic_error& ex)
+ {
+ SubHandlers::LogicError(ex);
+ }
+ catch (std::exception& ex)
+ {
+ SubHandlers::Exception(ex);
+ }
+ catch (...)
+ {
+ SubHandlers::Unknown();
+ }
+ }
+};
+
+template<class SubHandlers, class ReturnType=SubHandlers::return_t>
+struct CatchAllReturn
+{
+ static ReturnType Handler()
+ {
+ try
+ {
+ std::rethrow_exception(std::current_exception());
+ // Apparently VS 2012 does not realize that this function always throws.
+ // Unless we have an explicit return statement, we get a spurious warning C4715 "not all control paths return a value".
+ return ReturnType();
+ }
+ catch (std::system_error& ex)
+ {
+ return SubHandlers::SystemError(ex);
+ }
+ catch (std::runtime_error& ex)
+ {
+ return SubHandlers::RuntimeError(ex);
+ }
+ catch (std::logic_error& ex)
+ {
+ return SubHandlers::LogicError(ex);
+ }
+ catch (std::exception& ex)
+ {
+ return SubHandlers::Exception(ex);
+ }
+ catch (...)
+ {
+ return SubHandlers::Unknown();
+ }
+ }
+};
+
+/*
+ * NullHandlers is used at present to define the default exception behavior.
+ * When we get non-trivial default exception behavior, this class can move into the unit tests.
+ */
+struct NullHandlers
+{
+ static void Unknown() {}
+ static void Exception(std::exception& ex) {}
+ static void LogicError(std::logic_error& ex) { Exception(ex); }
+ static void RuntimeError(std::runtime_error& ex) { Exception(ex); }
+ static void SystemError(std::system_error& ex) { RuntimeError(ex); }
+};
+
+void DefaultExceptionBehavior()
+{
+ CatchAllVoid<NullHandlers>::Handler();
+}
+

Powered by Google App Engine
This is Rietveld