1

I need my methods to throw custom exceptions but it keeps giving me this error :
error C2059: syntax error : 'string'

I was reading the following link but it doesn't solve my problem:
http://msdn.microsoft.com/en-us/library/t8xe60cf%28VS.80%29.aspx

This is the code I have:

#include <exception>
#include <stdexcept>
#include <string>
#include "Log.h"

LOG_USE()

class Exception : public std::exception 
{
    public:
        explicit Exception(std::string msg)
            : message(msg)
        {}
        ~Exception()
        {}

        virtual const char* what() const  throw() 
        {
            LOG_MSG(message) // write to log file
            return message.c_str();
        }

    private:
        std::string message;
};

#endif

Somewhere in my app I have methods that look like this:

.....
....
void view::setDisplayView(ViewMode mode) throw(Exception("setDisplayView error"))
{
    ;
}
....
....

What am I doing wrong here?
I'm using Visual Studio 2008 on 32-bit Windows XP.

Jesse Good
  • 50,901
  • 14
  • 124
  • 166
user63898
  • 29,839
  • 85
  • 272
  • 514

1 Answers1

4

You're not using exception specification correctly. That throw, which follows the declaration of setDisplayView, should only contain a type (in your case, Exception), not an object (which is what you get with that Exception("setDisplayView error")).

Now, having that said, exception specifications are deprecated in C++11, and have not been considered a useful feature before. Better just omit that throw(Exception("setDisplayView error")). Use exception specifications only if you're not going to throw anything. In that case, the new syntax to use would be nothrow.

Edit:

To signify the exception has been thrown from setDisplayView, you must pass that information to the exception when it's created -

void view::setDisplayView(ViewMode mode)
{
     if (badThingHappened())
          throw Exception("setDisplayView error");
}

There are various non-standard techniques to find the origin of the exception when catching it, you can find some here. Using exception specification is just not one of them...

Community
  • 1
  • 1
Eran
  • 21,632
  • 6
  • 56
  • 89
  • Thanks now how can still catch exceptions that will be vaild in c++11 and standard c++ or just standard c++ ? for example how can i know in my exception class that the exception is coming from the setDisplayView method ? – user63898 Jul 02 '12 at 06:25
  • 1
    @user63898, this is not Java... You can throw whatever exceptions you want, without having to specify that anywhere (you might what to document it somehow, of course). Now, knowing your exception comes from a certain method requires some "manual work" - either use `throw Exception("setDisplayView error")` to throw the exception, or maybe create some macro that will pass some built-in macros like \_\_FILE\_\_, \_\_LINE\_\_ or \_\_FUNCTION\_\_ (if your compiler supports it). But exception specification will not do the trick here. – Eran Jul 02 '12 at 06:34
  • but i do throw Exception("setDisplayView error") and its not working inside the Exception class i have all the longing – user63898 Jul 02 '12 at 06:57
  • I'm talking about using `throw Exception("setDisplayView error")` _inside_ `view::setDisplayView`, at the exact place where you identify the error and would like to report it to the caller. Just remove the stuff between `view::setDisplayView(ViewMode mode)` and the `{`. – Eran Jul 02 '12 at 07:09
  • you mean try{ throw ... stuff inside the function . is this the only why ? – user63898 Jul 02 '12 at 11:48