Python Community Server: Development

A weblog about programming in Python, C#, Java, Perl and C++ - and the occasional comment on PyCS development
new: discuss community servers on the CommunityServerWiki!

SunMonTueWedThuFriSat
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

[ Sep ] [ Nov ]

the locals
also available in XML
Copyright (c) 2002 Phillip Pearson
spread the dot

2002-10-13

C++ exception handling tip

I've been spending far too much time debugging a simple C++ problem.  Here's a note for anyone else struggling with the same thing.

Basically, I've got some code that throws an exception.  I've got a hierarchy of exceptions, defined with code like this:

class Exception
{
    std::string _msg;
public:
    Exception( std::string msg ) : _msg( msg ) {}
};

class FooException : Exception
{
public:
    FooException() : Exception( "Foo" ) {}   
};

Now, an exception is thrown in some code:

void DoSomethingThatThrowsAnException()
{
    [do something]
    throw new BarException();
}

That code is run inside a block like this:

try
{
    DoSomethingThatThrowsAnException();
}
catch ( Exception* e )
{
    printf( "Something screwed up: %s\n", e->msg );
    delete e;
}

However, when I run this, I don't see the expected "Something screwed up: Foo!", but rather the following on Windows, and "Aborted" on Linux:

      6 [sig] test 2060 open_stackdumpfile: Dumping stack trace to test.exe.stackdump

It turns out that it's because I forgot to put in the 'public' keyword when deriving FooException.  This means that FooException wasn't caught by the try-catch block, because it didn't derive from Exception.

The correct code is:

class FooException : public Exception
{
public:
    FooException() : Exception( "Foo" ) {}   
};

Heh.  The moral of this story?  If your exceptions aren't being caught, check your types.  Are you catching what you're throwing?  You can catch all uncaught exceptions by putting in something like this (tag it on after the closing brace in the previous 'catch' block):

catch ( void* e )
{
    printf( "Uncaught exception - help!\n" );
}

This will let you know that the reason your program is dying horribly is because you forgot to catch something, not because of any of the other reasons C++ programs die with stack traces.

Footnote: I just clicked 'post' (I'm using SharpBlog to write this) and the following appeared:

      exceptions.NameError: name 'NotImplementedException' is not defined

Hooray for exceptions.  Once I'm done with the C++, it'll be time to go implement metaWeblog.updatePost() for bzero ;-)

Comment on this post [ so far]

Goodbye Rogers

Well, it looks like Rogers Cadenhead is moving out; instead of pycs.net/workbench, his blog will be located at cadenhead.org/workbench from now on.

He hasn't quite left yet, however - all his comments still live on pycs.net. The recent addition of comments to phpStorageSystem, however, means we should be able to transfer them over to his own server.

Comment on this post [ so far]