Like Tree1Likes
  • 1 Post By Codeplug

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

This is a discussion on Using <exception>, compiles in Visual Studio, massive errors in g++ within the C++ Programming forums, part of the General Programming Boards category; Hi I wrote some classes inheriting from <exception> in the following way: Code: class NoFile : public exception { public: ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    19

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

    Hi

    I wrote some classes inheriting from <exception> in the following way:
    Code:
    class NoFile : public exception
    {
    public:
    	NoFile();
    	NoFile(string);
    	NoFile(const NoFile &);
    	virtual ~NoFile();
    	NoFile operator=(const NoFile &);
    
    	virtual const char* what() const;
    	const char* getName() const;
    
    private:
    	const string reason;
    	string fileName;
    };
    And I use it in other files like that:
    Code:
    try
    	{
    		ifstream inp(inputName);
    		
    		if(inp.is_open() == true)
    		{
    			//do something
    		}
    		else
    		{
    			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:

    Code:
    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
    Posts
    4,691
    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.

    gg

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, 06:30 AM
  2. Visual Studio 2005 - Debug code with compile errors?
    By Swerve in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-17-2008, 09:12 AM
  3. Replies: 4
    Last Post: 04-26-2004, 07: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21