Thread: Using <exception>, compiles in Visual Studio, massive errors in g++

  1. #1
    Registered User
    Join Date
    Nov 2010

    Using <exception>, compiles in Visual Studio, massive errors in g++


    I wrote some classes inheriting from <exception> in the following way:
    class NoFile : public exception
    	NoFile(const NoFile &);
    	virtual ~NoFile();
    	NoFile operator=(const NoFile &);
    	virtual const char* what() const;
    	const char* getName() const;
    	const string reason;
    	string fileName;
    And I use it in other files like that:
    		ifstream inp(inputName);
    		if(inp.is_open() == true)
    			//do something
    			throw NoFile(this->inputName);
    	catch(const NoFile &e)
    		cerr << "Caught exception: " << e.what() << ": " << e.getName() << endl;
    		cerr << "Returning to main menu" << endl << endl;
    There are some more, but written in the same way so I guess there is no need to post them here. Anyway, the code for the entire program compiles without any problems in Visual Studio 2010 but the g++ compiler gives me millions of errors, most of them of this kind:

    error: looser throw specifier for 'virtual const char* NoFile::what() const
    Why this happens? Do VS and g++ have different implementations of <exception>? How to avoid these errors?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Under C++03, the exception::what() method looks like this:
    virtual const char* what() const throw();

    The throw specification at the end says "nothing will be thrown".

    When you leave the throw specification off, that says "anything or nothing can be thrown".

    When you override a virtual, you can't make the throw specification "lesser". So slap a "throw()" on your what() overrides as well.

    >> Do VS and g++ have different implementations ...
    They differ in that MSVC pretty much ignores throw specifications.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exception errors OR exception handling :S
    By cruiser in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2011, 05:30 AM
  2. Visual Studio 2005 - Debug code with compile errors?
    By Swerve in forum A Brief History of
    Replies: 2
    Last Post: 03-17-2008, 08:12 AM
  3. Replies: 4
    Last Post: 04-26-2004, 06:31 PM
  4. getting massive compile errors, need help
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2003, 04:05 PM
  5. Simple Program wont execute after compiles w. no errors
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 02-03-2002, 04:24 PM