A Convenient Exception Class

M

magnus.moraberg

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.
 
A

alasham.said

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" ) ;
}
 
V

Vidar Hasfjord

[...]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top