Using Exceptions in C++

March 8, 2010 – 11:40 pm

C++ is big – it has been said that any given programmer only ever uses about 40% of the language’s features. The trouble is that it is a different 40% for each person. Exceptions are a great example of this, some people swear by them while many coding standards specifically ban/discourage them (cf: google, mozilla). It is ironic that a feature designed to make code safer is sometimes regarded as being too dangerous to use.

Personally I like exceptions, but even I realise that they have there limitations. This post is an attempt to formalise some guidelines about when exceptions should be used and when they should be avoided beyond the usual language rules. I should mention at this stage that most of my experience is in desktop client/server software. C++ is used in all sorts of places these days, and what works on desktops and beefy servers may not suit the embedded world (for example).

When to Catch

My rule of thumb is “Do not let exceptions escape from a function you didn’t explicitly call yourself“. This includes destructors, callbacks, thread functions, WNDPROCs, and any other miscellaneous way your functions can be entered (it does not include constructors or virtual functions – exceptions are very useful in those). In general, all these things should catch and handle all exceptions.

Your program will die if an exception escapes a thread. If you are lucky your runtime will do something clever and your program will die painlessly but possibly the OS will have to dispatch the process messily. Either way, your users will not be impressed, so you should always wrap thread functions in try{}catch blocks. In the best case you might be able to signal that an operation failed to the main thread, which can restart it if required. In the worst case you can at least log what happened before exiting.

Lots of third part libraries communicate with your code using callbacks that you supply. You should always ensure that any exceptions are caught before returning back into third party code, since you can never be sure if the library does the right thing. C style libraries like LibCURL are right out, they will probably leak handles and memory as the stack is unwound. C++ libraries may (or may not) be better but could do things you do not expect, like swallow the exceptions themselves instead of letting them fall through (boost::iostreams). Also, some libraries actually call you back on a different thread (boost::asio) so the advice in the previous paragraph also applies here.

You should always be prepared to catch any exceptions that are documented by any C++ libraries you use, especially things like boost::filesystem which can throw at any time.

What to Throw

My advice is to create a small hierarchy that is derived from std::runtime_exception unless you are already using a custom exception class. Don’t try to get clever and throw std::string or char*. Design your hierarchy around how the exceptions are to be handled, rather than what can go wrong. For instance, if there are 5 different ways your program throw exceptions, but only 3 different things that can happen in response then you only need 3 types of exceptions.

In my experience, exceptions fall into two categories: recoverable and fatal. Recoverable exceptions will be caught within a layer, or perhaps the next layer up which can then reattempt the operation (or perhaps just log and ignore the problem if the operation was not crucial.) Fatal errors are usually not caught until the outer loop of the program, where they are logged before the program can be shutdown cleanly. In general, the exceptions you expect to recover from should derive from exceptions you expect to be fatal.

I tend to name my exception types based on how they are handled and what circumstance they represent, like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
class UnrecoverableFileReadException : public std::runtime_error
{
	// thrown when a file cannot be read correctly, ie: the file exists but is misformatted
public:
	UnrecoverableFileException( const std::string& msg ):std::runtime_error(msg) {};
};
 
class FileReadException : public UnrecoverableFileReadException
{
public:
	// thrown when a file cannot be read but the user can be prompted to select another file
	TemporaryFileException( const std::string& msg ):UnrecoverableFileReadException(msg) {};
};

When throwing, always construct the exception with a sensible message if only for logging and debugging purposes. Normally this message would not be shown to the user since it will be hard to localise. Add additional members to your exception class if you want to include other information with the exception. Just remember that exceptions must be copyable.

1
2
3
ostringstream oss;
oss << "Could not open file " << filename << " the error was: " << errno;
throw TemporaryFileException( oss.str() );

If you really want to go the whole hog consider using boost::exception which builds upon similar ideas.

When to Throw

Exceptions should never be thrown if everything is running perfectly. Only when something goes wrong should throwing an exception be considered an option. I have seen code that threw exceptions to signal the end of an enumeration or to signal that the results of a query were empty – I consider these examples to be a terrible use of the feature. Remember that an exception is not just an error, but something really outside of normal program flow.

I also tend not to use exceptions for logic errors. If an algorithm can fail then the calling code should be prepared to handle a return code signalling an error. Likewise I would not throw if a database query returned no results – an empty result set is inside the bounds of normal program flow. However, I would consider throwing an exception if the query failed due to the database being unavailable.

The best places to use exceptions are in situations where your program is using resources that are not under your control, including anything to do with IO. Both files and network connections exist outside of your program and can become unavailable at any point due to any number of reasons. In many cases the problems are transient and all your program needs to do is try again in a few minutes – a program that quits each time the DNS, directory server, or external database cannot be reached will not survive for very long in any production environment. Exceptions allow you to back out of an operation without too much trouble and handle the problem in a sensible location.

One problem you might encounter is that is often hard to retrofit exceptions into code that wasn’t designed for them. In this case, my advice about not letting exceptions fall through 3rd party code also applies to legacy code that you own. This is not to say that you cannot use exceptions at all, just that you may have to take steps to keep exception handling within the layers of your application.

A Trailer for Every Academy Award Winning Movie Ever

March 6, 2010 – 8:01 pm

It’s Oscar season; presented for your consideration:

Personally I think it is a shame they missed out the female lead’s gay male best friend who gives invaluable relationship advice and it really needs at least one dog.

Mobile Safari Does Not Support Flash (and Never Will)

February 16, 2010 – 10:35 pm

Listening to some people, the lack of Flash on the iPhone/iPad is some sort of crime against nature. There are numerous complaints about it online – all bemoaning the inability to play their favourite Flash games or view video. These complaints miss the point entirely – there are two simple reasons why Flash will never be supported in Mobile Safari.

The first reason is simple – Steve Jobs is a jealous God and thou shall have no other Gods besides Him. Apple created the App store so they would control the single way of getting software onto the device, being able to load a flash file from a browser completely circumvents this control. Simple.

Now we have that out of the way, we can move onto the second, more interesting, reason.

Flash would suck on the iPhone.

Lets talk about Flash video first. Most video sites use a custom Flash wrapper to display video in a sub-frame of the browser, with controls to zoom the video to full screen. The sub-frame is usually of a fixed size (640*360, etc) and surrounded by additional HTML (ads, links to other videos, etc). Straight away you should see the problem – the video is already bigger than the iPhones screen. Mobile Safari does an excellent job of resizing web pages, but that is going to leave you with a postage stamp sized video with even smaller controls. Going fullscreen may be a solution, if you can mange to tap the tiny button, but then you are not really using Flash as part of a web page anymore.

Back in the day (about 5 years ago), Flash video was a step above anything else on the web due to its widely deployed and not-too-bad codec. These days Flash is just a none-too-convenient way of displaying standard h264 files which the iPhone can play natively. Most of the big video sites have realized this and just serve the raw file to iPhones instead of trying to wrap it in a custom player, to the benefit of everyone.

(Drifting slightly off-topic for a moment, I imagine the use of Flash as a video player will start to decline even of desktops now that HTML5 is here with its useful <video> tag.)

Now lets talk about Flash games – Tower Defense, Crayon Physics, room escape puzzles, etc. I love them, you love them, everyone loves them. There is just one problem – none of the thousands of existing games would work on the iPhone even if Mobile Safari supported Flash perfectly!

The iPhone doesn’t have a keyboard, so most arcade-type games are right out. Even games that exclusively use the mouse would have problems since tapping your finger on the screen is much, much less precise than using a pointer. In addition, on the iPhone you effectively have multiple pointing devices – how would current Flash apps handle that?

For a quick demo of why sites like newsgrounds will never work on the iPhone, resize your browser window to 480*320 (or 320*480 since that is more usual) and visit your favourite gaming site. Now set your mouse pointer to a big white blob instead of an arrow to simulate tapping with a large figertip. Remember to stop playing after 45 minutes to replicate the battery drain. See how much fun you have.

UIButton.titleLabel is not as useful as it looks

February 13, 2010 – 12:53 pm

I have been doing some iPhone development lately. Nothing too amazing, just some test apps to get a feel for the system. Now, some people will tell you that Cocoa Touch is an API sent from God and frankly it is pretty good (especially given what passes for UI on other embedded devices), but that doesn’t mean it doesn’t have some annoyances.

Here is something that tripped me up for a while. The UIButton class has a property called titleLabel which (obviously) returns the UILabel that is used to display the text of the button. You can use this property to modify the parameters of the label, like so:

1
2
3
m_addButton.titleLabel.font = [UIFont systemFontOfSize: 7];
m_addButton.titleLabel.textColor = [UIColor blackColor];		 
m_addButton.titleLabel.textAlignment = UITextAlignmentRight;

What you can’t do is this:

1
m_addButton.titleLabel.text = @"Add Stuff";

Although nothing I have found in the documentation says so, the text of the button cannot be set from the titleLabel property. What you have to do is this:

1
[m_addButton setTitle:@"Add Stuff" forState: UIControlStateNormal];

Setting the title this way works, and has the advantage that you can specify different text for different states:

1
2
[m_addButton setTitle:@"Add Stuff" forState: UIControlStateNormal];
[m_addButton setTitle:@"Add Stuff (not now)" forState: UIControlStateDisabled];

This is perhaps not that interesting for text titles, but is an excellent way to control the image the button shows based on whether the button is enabled, highlighted, and/or selected.

The Aliens Rap

February 3, 2010 – 12:25 pm

Following up on the epic 10 minute rap summary of Robocop, the same team has released Aliens:


open at youtube.com

Watching this reminds me what a great film Aliens turned out to be and how Avatar (by the same director) pales in comparison. It’s not that Avatar was terrible but nobody is going to be making 10 minute rap songs about it in 25 years.

quick update: Hey, they’ve done Terminator 2 as well!

Film Review : Avatar

January 3, 2010 – 9:03 pm

James Cameron has always been an interesting film maker. Although on one level most of his films could be classified as pulpy genre-related fare, they usually have a more interesting subtext lurking below the explosions. Previous Cameron films have investigated such themes as mother/daughter relationships, humanity’s fear of the unknown, musings on fate and predestination, and whether it is morally acceptable (and perhaps even admirable) to slum it with a good looking lower class boy for a few weeks before you get married even though an ocean liner might not be the best place to do so. So it is with a heavy heart that I have to say Avatar is a slight disappointment.

avatarThe planet of Pandora (Who names these planets? What were they thinking?) has some stuff that humans want to mine. Unfortunately, the best place to get it is right on top of where the indigenous population (8 foot tall skinny blue people called the Navi) live in harmony with their world. The Navi are distrustful of the humans, so in order to investigate the Navi a bit more, the humans create the titular avatars – mindless Navi bodies that certain individuals can “drive around” remotely. The main character is just such an individual, and he (or his avatar) quickly becomes involved in the local tribe. Although the humans would prefer that the Navi move on without violence, it is clear that a military solution, led by a crazed marine, might be more expedient…

It is almost impossible to spoil anything about Avatar’s plot, no doubt you have already guessed the direction it which it unfolds. It is a shame that for all the risks involved in making what is apparently the most expensive movie ever made (it certainly looks like it), the story is as safe as an after-school special. The film could have made some interesting points about colonialism, or environmentalism, the military, or even feminism, but instead chooses to unspool a conventional yarn where the good guys are selfless and the bad guys are crazy and evil. It is not that is it a bad story per say, just something we have all seen many times before.

I saw Avatar in 3D, it is by far the best 3D experience so far. The lush jungles and mist-shrouded peaks of Pandora look amazing – Avatar is simply the greatest visual treat I have ever seen. The contrast between the sharp grey lines of the human base with the colourful, glowing environment outside is very well rendered. James Cameron has always been interested in portraying technology and Avatar is no exception – a nice touch is that all of the displays that the humans use during the movie are also in 3D. There are a thousand little details like that I loved about Avatar, it is just a shame that the whole thing isn’t as great as the sum of its parts. However, anyone who shares Cameron’s love for helicopters and giant robots and things being blown up by helicopters and giant robots will be thrilled.

Highly recommended if you can see it on the big screen in 3D. Otherwise only recommended if you like this sort of thing (but who doesn’t?)

The Phantom Menace Was Not a Very Good Movie

December 17, 2009 – 5:40 pm

I own all of the Star Wars DVDs except for one – The Phantom Menace. Even the weakest of the others have a certain charm, but TPM was stupid through and through. Even the title is stupid! I have yet to work out exactly what the titular menace actually was. Although the story includes several menaces, none of them seem particularly phantomastical. Unless the menace was supposed to be Palpatine’s amazingly convoluted plan, but that plot point doesn’t really bear fruit until the second film.

Anyway, I haven’t given The Phantom Menace much thought since it first came out but this guy certainly has:




Part 1
Part 2
Part 3
Part 4
Part 5
Part 6
Part 7

Even if you ignore the affectations of the reviewer, he has some pretty insightful points about how TPM fails as a movie.

The C++ Boost Libraries Part 6 – boost::any

December 6, 2009 – 3:41 pm

In C++ if you have a variable that you say is of type “Person” (for instance), you can be fairly certain (more or less) that it always actually contains a Person (or perhaps a subclass of Person. If you have a container of Persons, then you know (more or less) that every member is also a Person (or a subclass).

This is all very good, prevents a lot of runtime errors, and generally makes C++ a great language if you care about correctness. But sometimes, very rarely, you actually want to store a whole bunch of messy, unrelated types in a container without trying to ram them into some sort of class hierarchy. Parsers are a good example of this. It is often convenient just to chuck tokens of various types into a data structure for later processing without worrying too much about the specific type (string, int, float, etc).

boost::any is a small class that can hold values from almost any type, designed for just such messy applications. Using boost::any is very simple:

1
2
boost::any a1 = std::string("Moose");
boost::any a2 = 6;

Of course, getting the values back again is a little harder.

1
2
3
4
5
6
7
8
9
try
{
	std::string v1 = boost::any_cast< std::string >(a1); // this works, a1 is a string
	std::string v2 = boost::any_cast< std::string >(a2); // nope, will throw an exception at runtime
}
catch ( const boost::bad_any_cast& e )
{
	// tried to any_cast into something that wouldn't go
}

Of course, you can query a boost::any for the typeid of the stored object. Just don’t do it when Scott Meyers is in the vicinity.

1
2
3
4
5
std::string v;
if ( a1.type() == typeid(std::string) )
{
	v = boost::any_cast< std::string >( a1 ); // this should never throw, since we checked first
}

A single boost::any is perhaps not that useful, but a container of them can store almost anything we want:

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
typedef std::vector< boost::any > AnyVector;
AnyVector values;
 
values.push_back( 5 );
values.push_back( std::string("Hello") );
values.push_back( 5.3 );
 
try 
{
	for ( AnyVector::const_iterator p = values.begin();
			p != values.end();
			++p )
	{
		if ( p->type() == typeid(int) )
			cout << "Int = " << any_cast<int>(*p) << endl;
		else if ( p->type() == typeid(std::string) )
			cout << "String = " << any_cast<string>(*p) << endl;
		else 
		{
			cout << "Unhandled type: " << p->type().name() << endl;
		}
	}
} 
catch ( const boost::bad_any_cast &e )
{
	cout << "Bad any_cast<>" << e.what() << endl;
}

Any type that you put into a boost::any must be copy constructable (the any makes a copy, not a reference). You also have to make sure that its destructor doesn’t throw (but of course you do that anyway!)

Although I wouldn’t recommend boost::any for everyday use, it does come into its own when the only alternative is a huge class structure or (even worse) void *s.

Python and The Very Slow Server

November 28, 2009 – 2:23 pm

I don’t usually do a lot of Python programming, but I always enjoy it when the opportunity arises. Python is in no way a “clean” language, it has all sorts of warts and limitations that mean that it tends to not get used for big projects. Despite this (or maybe because of it), Python remains my go-to language for Getting Small Things Done Quickly. It is impossible to overstate the utility of just being able to start coding a function by bashing away at the python console – nothing else has given me the same sense of instant gratification since I started programming in BASIC back in the 80s.

The other big advantage of Python is the useful utility libraries that come with it as standard. Want to send twenty thousand emails? Just import smtplib. Want to generate code based on data from a spreadsheet? Import csv and away you go. Need a file that is exactly 32Mb is size? No problem. These are real examples from my job where Python has saved me many hours.

The most recent use I have put Python to is a slow server. For various murky and uninteresting reasons I need a rate-limiting HTTP server, one that I can easily control the speed at which it sends data. Enter Python’s very handy BaseHTTPServer module, which allows you to create custom HTTP servers with only a few lines of code by subclassing a request handler. Although the BaseHTTPServer is fairly useless for serving real files, it is perfect for this type of thing since it does all the boring work of parsing headers and returning status codes.

I don’t care about the contents of the data, just its size and how long it takes to serve. Since I will be varying these parameters a lot, I decided to make them part of each request so that each request could take a different amount of time – this means I don’t have to restart the server between each test run. Modifying the code to serve actual file data would be very simple.

I enjoyed writing this server so much that I regret that it didn’t take longer. Now I actually have to use it for its intended purpose, which I can assure you is not going to be as pleasant.

Here is the complete Python source:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# A very simple HTTP server designed to for testing situations where the data returned
# is not important but the rate at which it comes down is. This server can be started
# using the command: python delayserver.py
#
# Once started, it will listen for requests on port 8000
# Requests should be of the form http://<address>:8000/size=<bytes>,duration=<seconds>
# where: <bytes> is the size of the response data
# and    <seconds> is how long you want it to take (at minimum, it may take longer)
#
# Notes:
# * The timing is pretty inaccurate for small byte sizes, this isn't a problem for
#   what I need it for
# * Press ctrl-c to stop serving
 
 
import time
import BaseHTTPServer
 
class MyHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
 
	def do_GET(self):
		request = self.path.strip("/")
		duration = 1
		size = 1024
 
        	validRequest = False
		params = request.split(",")
		for p in params:
                    temp = p.partition("=")
                    if (temp[0] == "size"):
                        size = int(temp[2])
                        validRequest = True
                    elif (temp[0] == "duration"):
                        duration = int(temp[2])
                        validRequest = True
 
                if (validRequest == False):
                   self.send_error(404)
                   return
 
                self.send_response( 200 )
                self.send_header( "Content-Length", str(size) )
                self.send_header( "Pragma", "no-cache" )
                self.end_headers()
                self.slowWrite( self.wfile, size, duration )
 
 
        def slowWrite(self, output, size, duration):
                bytesWritten = 0
                startTime = time.time()
                while ( bytesWritten < size ):
                        now = time.time()
                        if (duration != 0):
                                desiredBytes = ( (now - startTime) / duration ) * size
                        else:
                                desiredBytes = size
                        desiredBytes = min( size, desiredBytes )
                        if (desiredBytes < bytesWritten ):
                                time.sleep(0.2)
                        else:
                                while (bytesWritten < desiredBytes):
                                        output.write('A')
                                        bytesWritten = bytesWritten + 1
                                output.flush()
                now = time.time()
                self.log_message( "Request took %f seconds",   now - startTime  )	
 
if __name__ == "__main__":
    http = BaseHTTPServer.HTTPServer( ('', 8000), MyHTTPRequestHandler )
    print "Listening on 8000 - press ctrl-c to stop"
    http.serve_forever()

I should point out that I am by no means an expert at Python, so take this code with a pinch of salt.

Pacman

November 27, 2009 – 11:00 am

Remember kids – winners don’t do drugs.

Catan – The First Island

November 3, 2009 – 9:38 pm

Catan ScreenshotI love the Settlers of Catan board game, so when an iPod version appeared in the App Store for $6.49 I grabbed it straight away. Dubbed “Catan – The First Island“, the app includes everything in the core game, I assume more games based on the Catan expansions are on the way.

The app plays a pretty good game, the interface is fairly straight forward and trading between players is handled well. Some rule variants are supported, like starting with a city instead of a settlement or different point targets. There are even different ways of distributing the resources for wusses who can’t take random chance. The computer players put up a fair challenge, and you can play hot-seat multiplayer but there is no internet play – an obvious omission.

Unfortunately, Catan also has some fairly annoying flaws. Firstly, the game is buggy. On both my first two games the computer player refused to finish its turn, leaving aborting the game the only option. The other 3 games I have had have worked smoothly so I still don’t know what I did to trigger that bug. Loading a saved game also sets some of the options back to the default, which is a pain but not game breaking.

Speaking of saved games, Catan does not automatically save your progress when you dismiss the app to go back to the iPod main menu. When you re-enter Catan it pops you back at the title screen with no way to resume unless you manually saved the game, something you can only do during your turn. This is intensely annoying, not to mention against Apple’s App guidelines and I hope the fix this if nothing else in an update.

The graphics are just OK, they get the job done without being very attractive and the board animation looks terrible. The whole package seems just a little unpolished – it works but needs just a little more attention to detail. The only thing that really saves Catan as an App is the mechanics of the game itself, which still shine. Perhaps after an update or two “Catan – The First Island” will reach its potential, but right now it is only for die-hard Catan fans.

A disappointment, only recommended if you like this sort of thing.

The Wellington Podcast

October 24, 2009 – 12:16 pm

Bevan McCabe, raconteur and man-about-town, is producing a podcast about this fair city. The first episode has just been released and it turned out rather well.

There is more information at the associated Wellington Podcast Blog, but the daring amongst you can just downloaded it directly (16Mb mp3), or subscribe to the Wellington Podcast using iTunes.

500 Impressions in 2 Minutes

October 19, 2009 – 11:01 pm


View at YouTube

This guy is incredible, how does he do that?

Also worth watching, from the same guy:
Wolverine in 30 Seconds
Unemployed Hitman
Terminator Salvation in 60 Seconds
StarTrek in 47 Seconds

Film Review : Moon

October 18, 2009 – 10:04 pm

Think your job is monotonous? Spare a thought for Sam Bell who comprises the entire crew of the lunar base that supplies the Earth with Helium3. Sam looks after the giant automatic mining machines that rove the Moon’s surface, sending back the recovered fuel to a world he hasn’t seen in years. The only thing to talk to is GERTY – a banal robot that helps to keep the base functional.

Hardly ideal working conditions, but if that wasn’t enough real-time communications with home have been cut off. Sam is only able to exchange short, recorded messages with his wife and mining company superiors, and the isolation is beginning to get to him. The good news is that he only has a few more weeks to go before his three year contract is up and he can return to his family. But even a week can be a long time for a man in such a fragile mental state…

For a low budget sci-fi film, Moon holds up very well. The tone is more reminiscent of more thoughtful films such as 2001 A Space Odyssey rather than the more usual alien infested fare. The plot moves at a fairly slow pace, but the lead actor (Sam Rockwell) and spectacular effects (the outdoor lunar surface looks particularly good) go a long way to make up for it. The one thing I thought was a little grating was that Sam was a fairly unlikable character to watch at times, stubborn and a little dim, but that can be explained by the tense situation – the film does a good job of putting you inside his one-note life.

Apparently Moon cost only 5 million dollars to make, but manages to both look more impressive and tell a better story than its blockbuster siblings. Highly recommended if you like this sort of thing.

Roller Derby

October 10, 2009 – 11:39 pm

Wellington is truly the cultural capital of NZ. Last weekend I attended the first art exhibition I have ever had to queue to get into; this weekend: Roller Derby!
Read the rest of this entry »