Thread: Problem with the exception class in MINGW

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    Problem with the exception class in MINGW

    I was trying to run a simple example in class based on exceptions. The teacher uses microsoft visual studio so I thought it was microsoft's implementation error rather than the actual mingw libraries. In the code it throws this exception.

    Code:
    int pop( )
    	{
    		if( empty( ) )
    			throw exception( "Attempt to pop from empty stack!" );
    
    		return stack[ --top ];
    	}
    but I get the error

    4338\exceptions\IntStack.h:39: error: no matching function for call to `std::exception::exception(const char[33])'

    53: note: candidates are: std::exception::exception(const std::exception&)

    55: note: std::exception::exception()

    158: error: `main' must return `int'
    :: === Build finished: 4 errors, 0 warnings ===
    I wondered if the example was wrong but whenI looked at the Mingw exception.h it only has this as the exception class declaration

    Code:
    class exception 
      {
      public:
        exception() throw() { }
        virtual ~exception() throw();
        /** Returns a C-style character string describing the general cause
         *  of the current error.  */
        virtual const char* what() const throw();
      };
    but when I looked on the MSND library it has this for the exception class it has it declared as this...do I have the wrong class or something?

    Code:
    class exception 
      {
      public:
        exception() throw() { }
        virtual ~exception() throw();
        /** Returns a C-style character string describing the general cause
         *  of the current error.  */
        virtual const char* what() const throw();
      };

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    no matching function for call to `std::exception::exception(const char[33])'
    Look at the exception class definition you posted. Is there a constructor that takes a string as an argument?
    note: candidates are: std::exception::exception(const std::exception&)
    Another candidate would be the default exception constructor. So, you can either throw an exception object created with the default exception constructor, or you have to derive your own class from the exception class and throw an object from that class(which would have the type exception&).
    Last edited by 7stud; 01-19-2007 at 10:59 PM.

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I wonder why it's that way in the example.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Your teacher made a mistake. You can write:
    Code:
    throw "some string";
    A simple example wouldn't involve creating and throwing an exception object(have you even studied objects yet?) Here is what a simple example would look like:
    Code:
    #include <iostream>
    
    using namespace std;
    
    void someFunc(int num)
    {
    	try
    	{
    		if(num == 0)
    		{
    			throw "division by zero error in someFunc()";
    		}
    
    		double result = 10/num;
    		cout<<result<<endl;
    	}
    	catch(const char* message)
    	{
    		cout<<message<<endl;
    	}
    	
    	
    	
    }
    
    int main()
    {
    	someFunc(0);
    
    	return 0;
    }

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Or you can do this. Probably better than throwing a string.
    Code:
    #include <stdexcept>
    
    void foo()
    {
      throw std::runtime_error("runtime_error has a constructor accepting strings");
    }
    Because runtime_error is derived from exception, you can use the same catching code.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    So far it's the only error I've found in the examples (aside from the strongly disadvised void main which he doesn't mind students using, but he acknowledged when and why int main is used, I don't use it though)

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by indigo0086
    but he acknowledged when and why int main is used
    Apparently not. The rule is simple:
    int main is used always and everywhere, unless the program uses a different entry point altogether (such as WinMain).
    In C++, there's absolutely no excuse for using void main instead of int main. You don't even save yourself the return value, as C++ automatically returns 0 from main. All you get is an additional character in the function header.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with friend class declaration in a namespace
    By Angus in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2008, 01:29 PM
  2. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 09:42 PM
  3. problem with class
    By Mr.Pink in forum C++ Programming
    Replies: 26
    Last Post: 07-10-2005, 10:24 PM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  5. static class problem.
    By Sebastiani in forum C++ Programming
    Replies: 3
    Last Post: 10-16-2002, 03:27 PM