LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-19-2005, 03:43 AM   #1
qwijibow
Guru
 
Registered: Apr 2003
Location: nottingham england
Distribution: Gentoo
Posts: 2,672

Rep: Reputation: 47
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.
 
Old 04-19-2005, 06:08 AM   #2
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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.

Last edited by Hivemind; 04-19-2005 at 06:15 AM.
 
Old 04-19-2005, 04:36 PM   #3
qwijibow
Guru
 
Registered: Apr 2003
Location: nottingham england
Distribution: Gentoo
Posts: 2,672

Original Poster
Rep: Reputation: 47
Ahh perfect, thanks alot.
 
Old 04-20-2005, 05:16 AM   #4
qwijibow
Guru
 
Registered: Apr 2003
Location: nottingham england
Distribution: Gentoo
Posts: 2,672

Original Poster
Rep: Reputation: 47
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 !

Last edited by qwijibow; 04-20-2005 at 05:34 AM.
 
Old 04-20-2005, 05:23 AM   #5
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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.

Last edited by Hivemind; 04-20-2005 at 05:24 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception Handling vipinsharma Programming 2 09-25-2005 11:05 AM
Exception Handling C++ Hady Programming 3 07-26-2005 11:07 PM
CPU exception zaicheke Linux - Hardware 3 11-26-2004 11:37 AM
Runtime Exception vs. Exception mikeshn Programming 1 09-22-2002 05:33 AM
c++ : regarding (inheritence)base class and derived class edreddy Programming 6 07-31-2002 06:33 PM


All times are GMT -5. The time now is 03:06 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration