lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [5983] Increase safety


From: Greg Chicares
Subject: [lmi-commits] [5983] Increase safety
Date: Sun, 12 Oct 2014 15:09:17 +0000

Revision: 5983
          http://svn.sv.gnu.org/viewvc/?view=rev&root=lmi&revision=5983
Author:   chicares
Date:     2014-10-12 15:09:15 +0000 (Sun, 12 Oct 2014)
Log Message:
-----------
Increase safety

Modified Paths:
--------------
    lmi/trunk/ChangeLog
    lmi/trunk/handle_exceptions.hpp
    lmi/trunk/handle_exceptions_test.cpp
    lmi/trunk/main_wx.cpp

Modified: lmi/trunk/ChangeLog
===================================================================
--- lmi/trunk/ChangeLog 2014-10-12 01:36:18 UTC (rev 5982)
+++ lmi/trunk/ChangeLog 2014-10-12 15:09:15 UTC (rev 5983)
@@ -34311,3 +34311,11 @@
 Improve exception class added 20141011T1234Z. See:
   http://lists.nongnu.org/archive/html/lmi/2014-10/msg00043.html
 
+20141012T1509Z <address@hidden> [533]
+
+  handle_exceptions.hpp
+  handle_exceptions_test.cpp
+  main_wx.cpp
+Increase safety. See:
+  http://lists.nongnu.org/archive/html/lmi/2014-10/msg00041.html
+

Modified: lmi/trunk/handle_exceptions.hpp
===================================================================
--- lmi/trunk/handle_exceptions.hpp     2014-10-12 01:36:18 UTC (rev 5982)
+++ lmi/trunk/handle_exceptions.hpp     2014-10-12 15:09:15 UTC (rev 5983)
@@ -61,7 +61,7 @@
 /// Implicitly-declared special member functions do the right thing.
 
 class stealth_exception
-  :public std::runtime_error
+    :public std::runtime_error
 {
   public:
     explicit stealth_exception(std::string const& what_arg);
@@ -74,6 +74,18 @@
 /// once and only once. See:
 ///   
http://groups.google.com/group/comp.lang.c++.moderated/msg/7ac8db2c59c34103
 ///
+/// Simply rethrow when stealth_exception is caught: see the comments
+/// accompanying its declaration. To catch even stealth_exception and
+/// classes derived from it--for instance, in a main() function, where
+/// an untrapped exception would cause a crash--write:
+///   catch(...) { try{report_exception();} catch(...){/*warning*/} }
+///
+/// Show no message when hobsons_choice_exception is caught--just
+/// swallow it silently. It's thrown only when
+///  - an appropriate message was just shown, and then
+///  - the safe default action (throwing this exception) was accepted,
+/// in which case it's pointless to repeat the same message.
+///
 /// It may seem like a good idea to test std::uncaught_exception()
 /// right before the try block, as recommended here:
 ///   
http://groups.google.com/group/comp.lang.c++.moderated/msg/ec0ef69dd3949955
@@ -86,15 +98,6 @@
 ///   "The only problem with uncaught_exception is that it doesn't
 ///   tell you when you're in a catch(...) { ... throw; } block"
 ///
-/// Simply rethrow when stealth_exception is caught: see the comments
-/// accompanying its declaration.
-///
-/// Show no message when hobsons_choice_exception is caught. It's
-/// thrown only when
-///  - an appropriate message was just shown, and then
-///  - the safe default action (throwing this exception) was accepted,
-/// in which case it's pointless to repeat the same message.
-///
 /// See
 ///  http://article.gmane.org/gmane.comp.gnu.mingw.user/18355
 ///    [2005-12-16T09:20:33Z from Greg Chicares]

Modified: lmi/trunk/handle_exceptions_test.cpp
===================================================================
--- lmi/trunk/handle_exceptions_test.cpp        2014-10-12 01:36:18 UTC (rev 
5982)
+++ lmi/trunk/handle_exceptions_test.cpp        2014-10-12 15:09:15 UTC (rev 
5983)
@@ -32,18 +32,56 @@
 
 #include <stdexcept>
 
+/// This function is normally unimplemented.
+
+stealth_exception::stealth_exception(std::string const& what_arg)
+    :std::runtime_error(what_arg)
+{}
+
+class sneaky_exception
+    :public stealth_exception
+{
+  public:
+    explicit sneaky_exception(std::string const& what_arg)
+        :stealth_exception(what_arg)
+    {}
+};
+
 int test_main(int, char*[])
 {
     try
         {
-        BOOST_TEST(true);
         throw std::runtime_error("  This message should appear on stderr.");
         }
     catch(...)
         {
         report_exception();
+        BOOST_TEST(true);
         }
 
+    // Test the
+    //   catch(...) { try{report_exception();} catch(...){/*warning*/} }
+    // technique to trap every exception--even stealth_exception or an
+    // exception derived from it.
+    try
+        {
+        std::cout << "Expect a success message..." << std::endl;
+        throw sneaky_exception("ERROR");
+        }
+    catch(...)
+        {
+        try
+            {
+            report_exception(); // Should rethrow.
+            BOOST_TEST(false);
+            }
+        catch(...)
+            {
+            std::cout << "...Success: caught elusive exception." << std::endl;
+            BOOST_TEST(true);
+            }
+        }
+
     return 0;
 }
 

Modified: lmi/trunk/main_wx.cpp
===================================================================
--- lmi/trunk/main_wx.cpp       2014-10-12 01:36:18 UTC (rev 5982)
+++ lmi/trunk/main_wx.cpp       2014-10-12 15:09:15 UTC (rev 5983)
@@ -36,6 +36,7 @@
 #   pragma hdrstop
 #endif
 
+#include "alert.hpp"                    // safely_show_message()
 #include "fenv_lmi.hpp"
 #include "force_linking.hpp"
 #include "handle_exceptions.hpp"
@@ -100,7 +101,14 @@
         }
     catch(...)
         {
-        report_exception();
+        try
+            {
+            report_exception();
+            }
+        catch(...)
+            {
+            safely_show_message("Logic error: untrapped exception.");
+            }
         }
 
     fenv_validate();




reply via email to

[Prev in Thread] Current Thread [Next in Thread]