LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   help createing exception class from base STL exception (http://www.linuxquestions.org/questions/programming-9/help-createing-exception-class-from-base-stl-exception-314790/)

qwijibow 04-19-2005 03:43 AM

help createing exception class from base STL exception
 
For my c++ course work, i need to complete a partially written c++ porgram.

this program throws an exception class called "interpreter_exception".
i need to write this exception class.

ive noticed that in the catch blocks, this exception is treated very similar, if not exactly the same as the STL exception class, so im thinking it would be a good idea to make interpreter_exception a derived class, from super class stl::exception.

what do i need to do in order to make this work ? are there any virtual / pure virtual functions i will ned to define ? or will a simple

Code:

class interpreter_exception : public stl::exception {};
work fine ???

thanks.

Hivemind 04-19-2005 06:08 AM

Standard way:
Code:

class interpreter_exception : public std::exception
{
public:
  explicit interpreter_exception(const std::string& what)
      :
      m_what(what)
  {}

  virtual ~interpreter_exception() throw() {}

  virtual const char * what() const throw()
  {
      return m_what.c_str();
  }

private:
  std::string m_what;
};

Note that the base class for exceptions, namely exception, lives in namespace std:: on a conforming compiler, not in the non-standard namespace stl::. Also note my use of explicit constructors and my throw() specifiers for the destructor and the what() function, indicating they themselves may not throw any exceptions.

qwijibow 04-19-2005 04:36 PM

Ahh perfect, thanks alot.

qwijibow 04-20-2005 05:16 AM

OOPS !
correction.... its the std::runtime_error that i should inheit from, not std::exception.

but i think the code you gave me is the same, except i need to use a iffernent library < stdexcept > instad of < exception >

edit:

oops, nope. my following class generate the errors shown...

Code:

#ifndef CXS12U_EXCEPTIONS_HPP
#define CXS12U_EXCEPTIONS_HPP

#include<exception>
#include <stdexcept>

namespace jminus {

class interpreter_exception : public std::runtime_error {

private:
        std::string m_what;

public:

        explicit interpreter_exception(const std::string& what) : m_what(what) {
                //m_what = what;
        }

        ~interpreter_exception() throw() {}

        const char* what() const throw() {
                return m_what.c_str();
        }
};

}
 
#endif

Code:

bash-2.05b$ g++ src/main.cpp -o bin/jminus
In file included from src/main.cpp:12:
src/jminus/exceptions.hpp: In constructor `jminus::interpreter_exception::interpreter_exception(const std::string&)':
src/jminus/exceptions.hpp:17: error: no matching function for call to `std::runtime_error::runtime_error()'
/usr/lib/gcc/x86_64-pc-linux-gnu/3.4.3/include/g++-v3/stdexcept:109: note: candidates are: std::runtime_error::runtime_error(const std::runtime_error&)
/usr/lib/gcc/x86_64-pc-linux-gnu/3.4.3/include/g++-v3/stdexcept:115: note:                std::runtime_error::runtime_error(const std::string&)

any idea's ??

If i could find decent documentation on etd::exception class, i could do all this myself.

my c++ book is quite old, and only goes as far as throwing intergers floats, bools and char's for exceptions !

Hivemind 04-20-2005 05:23 AM

std::runtime_error is a subclass of std::exception. If you inherit from std::runtime_error instead, the code can be simplified somewhat:

Code:

class interpreter_exception : public std::runtime_error
{
public:
  explicit interpreter_exception(const std::string& what)
      :
      std::runtime_error(what)
  {}

  virtual ~interpreter_exception() throw() {}
};

Note that there is now no need to store the what-string ourselves or to redefine the what-function (but we could). This is all handled by std::runtime_error instead. But a class like this really doesn't add much except the ability to distinguish between different exceptions using the type information. So if all you do is display what what() returns then exit, a class like this is not very useful. If, however, you take different actions regarding different types of exceptions, it is useful.


All times are GMT -5. The time now is 07:25 AM.