Thought for the Dazed

I've had to give up that Distance Learning course as I was having trouble seeing the teacher.

Flickr
www.flickr.com
RobMiles' items Go to RobMiles' photostream
Twitter
C# Yellow Book

Search entire site
Monday
May172010

Watch this, and then Watch it Again

 

Thanks to Ginny for the link via Twitter. Find out more here.

Monday
May172010

C# Exceptions and Finally

London Tulips

I’m writing a new version of the C# Yellow Book and I’ve been going through the chapters to make sure they still make sense. Today I’ve been playing with Exceptions. These are things that programs do when really bad things happen. When things are going badly for me in a chess game I have this annoying habit of kicking the board over and storming of to do something else. Exceptions are just like that, but for programs.

If a program finds itself in a situation where it really can’t go any further it can throw an exception and make the issue somebody else’s problem. Really bad in this case means things like “I asked for a number and you’ve given me some text” or “You asked for a file and I can’t find it”.  This means that a proper programmer will put such potentially moody code into a try – catch construction so that they can deal with any tantrums that might be thrown:

try
{
    // Potentially moody code
}
catch
{
    // Code to recover the situation
}

If anything in the block under the try throws an exception the program runs the code in the catch block to try and put things right. When I was younger and more desperate to be liked I used to make sure that code I wrote didn’t ever throw exceptions, but tried to sort things out and carry on. Ask my object to load something from a file that doesn’t exist and it would return a null reference and keep going. Sure, the program that went on to use that null reference would explode some time afterwards, but at least my method hadn’t thrown a tantrum.

Now I’m more nasty. I let my programs throw exceptions if they are asked to do stupid things. That way the fault is generated much closer to the real reason why it happened. Problem is, that sometimes I don’t want to just pass an exception on, I want to do a bit of tidying up myself before I blow away the person that calls me. I can do this by catching the exception in my method and then throwing a new one.

try
{
    // Potentially moody code
    // Code to tidy things up
}
catch
{
    // Code to tidy things up
    throw new Exception ("Bad news message to caller");
}

Of course the problem here is that I have to write the code to tidy things up in more than one place. Once if everything works, and again if the exception is thrown. Which is where finally comes in:

try
{
    // Potentially moody code
}
catch
{
    throw new Exception ("Bad news message to caller");
}
finally
{
    // Code to tidy things up
}

You don’t have to add a finally part to your try – catch construction, but if you do it is where you should put all your tidy up code. This is the code that must run whether or not the exception is thrown, and irrespective of what happens in the blocks above. I use code in the finally part to do things like close files, release objects and say bye-bye to network connections. And it works well except in one situation, which is kind of why I’m writing this blog post.

The code in my finally clause will not run if there is nothing to catch any exceptions thrown by the code in my try – catch construction. In other words, if the exception my code throws will cause the program to end for lack of an exception handler the finally clause will not run. I did some experiments to prove this, and decided it was something worth noting.

The thinking, I suppose, is that if the program is going to stop anyway, what is the point of allowing it to run any more code. Most of the time the finally clause will be releasing resources that will be freed anyway when the program is terminated. However, it is not always as simple as that. Sometimes finally code might want to say a formal goodbye to a remote host, or log some information. 

Since you can’t rely on someone to always catch exceptions that you might throw (particularly if they are stupid enough to make bad calls of your methods in the first place) I think that this means you need to be a bit careful about using finally. If this means putting tidy up code into a method and then calling it both in the try and catch parts of your program, this might be a step worth taking.

Sunday
May162010

Financial Crash

London Trafalger Square Fountains

Last week I paid off the wrong credit card by mistake and I now have a positive amount of money on it. I’m going to sign up for a balance transfer and see if it brings down the entire banking system.

Saturday
May152010

London Town

London Trafalger Square

Trafalgar Square and the National Gallery

Went to London today. Early start on the train and then a good walk round. The rest of the family acquired some culture at Wicked while myself and number one son took in the gadgets on Tottenham Court road.

London Horseguard

Horse guard

London Look Right

Kerbside political comment

London Big Ben Clock

Not sure where the other hand went….

Friday
May142010

Read “Bad Science”

I was talking to some folks today and they told me they had not read “Bad Science”. They should. There are some books that everyone should read, and this is one of them. It does a great job of getting you to think properly about stuff you see in the papers, and things that advertisers tell you. It makes warning bells come on in your head when you hear things like “Studies show…” and “90% of women prefer….”.

Brilliant.