XP Meetup: 97 Things Every Programmer Should Know


I know this is cheating, I am supposed to blog on Fridays, but I am really excited about Monday’s XP Meetup. Kevlin Henney is going to present his new book 97 Things Every Programmer Should Know: Collective Wisdom from the Experts, together with some of the Scandinavian contributors.

I have seen Kevlin in a couple of courses at work, and also a couple of times at JavaZone. He is both knowledgeable, fun and engaging, so this should be a great night.

This is happening at Radisson Blu Scandinavia Hotel in Oslo, Monday 22 March at 6:30 PM, and there are still 14 more free seats! If you want to attend, join Oslo XP Meetup on meetup.com.

The returning function that never returned


Investigating a crash report yesterday, I came across this piece of code (simplified for the purpose of this post):

std::string foo() try {
    bar();
    return "foo";
} catch (...) {
    log("Unable to foo!")
}

int main() {
    std::string s = foo();
}

Today’s exercise: what happens if bar() throws? Obviously, the function doesn’t return, since the exception is thrown and control is passed to the catch block. But since this block does not return, and does not rethrow, foo() neither returns nor throws, so what goes into the string s? Answer: No one knows, we are left in the happy land called undefined behaviour.

The morale? If the original implementor had used -Wall, he would have gotten a warning that execution reaches the end of the non-void function. (Both Sun Studio and gcc happlily compiles without complaining otherwise.)

Update:I have posted a follow up going into more details on function try blocks.

If you enjoyed this post, you can subscribe to my blog, or follow me on Twitter