Page 1 of 1

std::exception?

Posted: Wed Aug 12, 2009 20:55
by Jamarr
I noticed in v0.6 that CEGUI::Exception is not derived from std::exception - is there a particular reason for this? And would you consider making this an option? I would rather like to avoid having multiple catch statments because CEGUI is the only library I use that does not derive it's exceptions from std::exception...ex:

Code: Select all

try{
...
}catch(CEGUI::Exception& e){
...cegui-handler...
}catch(std::exception& e){
...all-other-std-derived-handler...
}


Maybe something as simple as (note I haven't tested this):

Code: Select all

diff --git a/CEGUI/include/CEGUIExceptions.h b/CEGUI/include/CEGUIExceptions.h
--- a/CEGUI/include/CEGUIExceptions.h
+++ b/CEGUI/include/CEGUIExceptions.h
@@ -52,7 +52,10 @@
     \brief
     Root exception class used within the GUI system.
     */
-    class  CEGUIEXPORT Exception
+   class  CEGUIEXPORT Exception
+#ifdef CEGUI_STD_EXCEPTION
+      :public std::exception
+#endif
     {
 
     public:
@@ -71,6 +74,15 @@
 
         /*!
         \brief
+        Return a c-string describing the exception being thrown.
+
+        \return
+        c-string containing a message describing the exception.
+        */
+      virtual const char* what() const;
+
+        /*!
+        \brief
         Return a reference to the String object describing the reason for the exception being thrown.
 
         \return
@@ -136,6 +148,12 @@
 
         /*!
         \brief
+        Holds the default error message.
+        */
+        String   d_what;
+
+        /*!
+        \brief
         Holds the reason for the exception being thrown.
         */
         String   d_message;

Code: Select all

diff --git a/CEGUI/src/CEGUIExceptions.cpp b/CEGUI/src/CEGUIExceptions.cpp
--- a/CEGUI/src/CEGUIExceptions.cpp
+++ b/CEGUI/src/CEGUIExceptions.cpp
@@ -52,7 +52,9 @@
     Exception::Exception(const String& message, const String& name, const String& filename, int line)
         : d_message(message), d_filename(filename), d_name(name), d_line(line)
     {
+      d_what = name + " in file " + filename  + "(" + PropertyHelper::intToString(line) + ") : " + message;
+
         // Log exception or send it to error stream (if logger not available)
         Logger* logger = Logger::getSingletonPtr();
         if (logger)
         {
@@ -55,8 +57,8 @@
         // Log exception or send it to error stream (if logger not available)
         Logger* logger = Logger::getSingletonPtr();
         if (logger)
         {
-            logger->logEvent(name + " in file " + filename  + "(" + PropertyHelper::intToString(line) + ") : " + message, Errors);
+            logger->logEvent(d_what, Errors);
         }
         else
         {
@@ -60,7 +62,7 @@
         }
         else
         {
-            std::cerr << name << " in file " << filename.c_str() << "(" << line << ") : " << message.c_str() << std::endl;
+            std::cerr << d_what.c_str() << std::endl;
         }
     }
 
@@ -69,4 +71,9 @@
     {
     }
 
+   const char* Exception::what() const
+   {
+      return d_what.c_str();
+   }
+
 } // End of  CEGUI namespace section

Re: std::exception?

Posted: Thu Aug 13, 2009 08:42
by CrazyEddie
Jamarr wrote:I noticed in v0.6 that CEGUI::Exception is not derived from std::exception - is there a particular reason for this?

There's no special reason for this; it just seemed like the right thing at the time. The question has actually been raised a couple of times before, and while we never had a great resistance to the idea, nothing ever got done about making the change.
Jamarr wrote:And would you consider making this an option?

Certainly. Are you looking for such a change in the v0-6 branch or the trunk? Bearing in mind that there will be no more releases in the 0.6.x series.

CE.

Re: std::exception?

Posted: Thu Aug 13, 2009 16:02
by Jamarr
trunk

Re: std::exception?

Posted: Fri Aug 14, 2009 10:38
by CrazyEddie
I'll get it done over this weekend :)

CE.

Re: std::exception?

Posted: Sun Aug 16, 2009 15:56
by CrazyEddie
I've added this in trunk @ rev 2159.

Ultimately I decided not to go with an option, but just to make the subclassing permanent. One reason for this was to avoid a compiler conditional in the public header, the other reason was that there is no compelling reason not to have this (one of the only reasons I didn't do it previously was to avoid breaking stuff at a time when I was looking to not break stuff - in the current trunk code, I already broke everything else, so doing this now will not make much difference!).

CE.