Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A Convenient Exception Class

0 views
Skip to first unread message

magnus....@gmail.com

unread,
Jan 27, 2009, 4:51:20 AM1/27/09
to
Hi,

When I create a class, I normally give it an exception Class -

class MyClass
{
private:
MyClassExpection(): public runtime_error
{
public:
MyClassExpection(const string& what) :
runtime_error("Exception within MyClass: " + what)
{
}
};
};

I can then use this class as follows within MyClass scope -

stringstream failStringStream;
failStringStream << "The Step Id: " << (unsigned int)step << " is
invalid";
throw MyClassExpection(failStringStream.str());

But I was wondering if I could add another constructor to my exception
class as follows -

MyClassExpection(const stringstream& what) :
runtime_error("Exception within MyClass: " + stringstream.str
())
{
}

and then use it like this -

throw MyClassExpection( stringstream() << "The Step Id: " <<
(unsigned int)step << " is invalid" );

or the less clear -

throw MyClassExpection( stringstream("The Step Id: ") << (unsigned
int)step << " is invalid" );

but these give a compiler error -

no matching function for call to
‘FS::MyClass::MyClassExpection::MyClassExpection
(std::basic_ostream<char, std::char_traits<char> >&)’

This does work -

stringstream failStringStream;
failStringStream << "The Step Id: " << (unsigned int)step << " is
invalid";
throw MyClassExpection(failStringStream);

why do I get this error?

are there any better solutions to the issue I'm trying to solve. That
is, the creation of a convenient exception object on one line.

Thanks,

Barry.


alasha...@gmail.com

unread,
Jan 27, 2009, 7:08:13 AM1/27/09
to
Hello,

The following should work. Regards

#include <sstream>
#include <stdexcept>

class MyClassExpection : std::runtime_error {
public:
explicit MyClassExpection( const std::basic_ostream<char,
std::char_traits<char> >& what ) :
std::runtime_error( "Exception within MyClass: " +
static_cast<const std::stringstream&>( what ).str() )
{ }
};

int main()
{
unsigned int step = 0;
throw MyClassExpection( std::stringstream("The Step Id: ") << step
<< " is invalid" ) ;
}

Vidar Hasfjord

unread,
Jan 27, 2009, 8:05:59 AM1/27/09
to
On Jan 27, 9:51 am, magnus.morab...@gmail.com wrote:
> [...]

> But I was wondering if I could add another constructor to my exception
> class as follows -
>
> MyClassExpection(const stringstream& what) :
>         runtime_error("Exception within MyClass: " + stringstream.str
> ())
>         {
>         }
>
> and then use it like this -
>
> throw MyClassExpection(   stringstream() << "The Step Id: " <<
> (unsigned int)step << " is invalid"  );

I don't think it is a good idea to mix this string formatting feature
with your exceptions. They are really separate concerns. For a
convenient general inline string builder, see:

http://groups.google.com/group/comp.lang.c++.moderated/msg/a400421feca71ea1

This thread also explains the problems you encounter.

Regards,
Vidar Hasfjord

0 new messages