Monday, January 19, 2009

Exceptions Revisited

Exceptions are an integral part of any modern programming language. AS a mover from Java to .Net I have had my fair share of exception handling changes. In Java you have both checked and unchecked exceptions and the throws exception clause is part of the method signature. In .Net (C#) all exceptions are unchecked which was kind of weird to me at the start. But after programming with .Net for over 2 years now I'm pretty convinced that the unchecked exceptions are the way to go. But I came across Spec#, an experimental extension language to C# by Microsoft has decided to give checked exceptions another go and thus I thought I'd revisit the whole argument again.

When the chief architect of C# language,
Anders Hejlsberg was asked why they opted to keep checked exceptions (which he recalls as 'handcuffs' on programmers ) he sighted 2 main reasons.
  1. Scalability
  2. Versioning
What he meant by scalability is the fact that when you write programmes which interacts with a whole bunch of other subsystems and APIs the number of exceptions the programmer is forced to handle grows out of control. When you just deal with the IO namespace in .Net framework this is not so much of an issue as you only have to handle a few IO exceptions but in large systems you can end up having to handle tens and hundreds of exceptions. So what happens is that the programmer gets tired of this and writes a big catch Exception clause which completely defeats the purposes of having checked exceptions in the first place. Or even worst they have the empty curly braces for each catch clause which is basically a planted time bomb. Another valid tactic people use is catching all checked exceptions and to wrap them as Runtime exceptions making sure that they no longer obstruct the users on top of the chain.

Under 'Versioning' the problem is that the throws clause is part of the method signature. So when you release a second version of an API or class library you can not introduce new exceptions or change existing ones since it will break client code. You are now left with having to declare a new method with a different set of exceptions which makes the API messy and unclean.

On the other hand one of the main arguments against unchecked exceptions is that the client programmer has to solely depend on the documentation of the API method to know what exceptions can be thrown by the function. But you can have a tool do this analysis for you like the 'Exception Hunter' from RedGate Software. According to Hejlsberg the most important thing about exceptions is not how you handle them, but how you release resource upon encountering one. This is perfectly handled by the try-finally blocks or using blocks in C# (Disposable pattern). The reason is that only a fraction of exceptions caught really need or can be handled by client code. The proven practice of dealing with unchecked exceptions is to let them propagate to the top of your call stack and handle them in a single place. By handling what is usually being done is a message to the user and graceful termination of the programme. We do this in very simple way in ASP.Net applications by using the Global.asax Application_Error handler.



Tuesday, January 13, 2009

War and Peace


One of the most die-hard critics of current Sri Lankan government, Lasantha Wickramatunga was shot dead last week. He was the chief editor of Sunday Leader, a weekly newspaper in English. I must say that I have not read the Leader for a very long time and not a fan of Lasantha by any mean, but still I was deeply shocked at his demise. Although I have many a reservations about his never ending conspiracy stories , without a doubt he was the only main stream journalist in South who criticized the highly glorified war currently run by the SL government. Although the country is going steeply downward in terms of democracy almost the entire majority Sinhalese is glued to the war heroics like we are glued to a game of cricket won by the Sri Lankan cricket team.


The author of the attached EDITORIAL is ‘Dead Lasantha’ not the living one which makes its points send a more chilling wave through my spine than ever. The writing lists 4 very important assets of good governance.

1. Transparent

2. Secular

3. Liberal

4. Democratic

Please get away from the Wins in war fields for a second and give it a thought. Does your educated mind allow you to do that you?


First they came for the Jews
and I did not speak out because I was not a Jew.

Then they came for the Communists
and I did not speak out because I was not a Communist.

Then they came for the trade unionists
and I did not speak out because I was not a trade unionist.

Then they came for me
and there was no one left to speak out for me


German Theologian,
Martin Niemöller
@ the time of the Great Holocaust

Friday, January 02, 2009

Gran Torino



As of things to expect during the new year of 2009 in the very first day of the year, I had to go through an absolute crap bag of a movie and followed it up with one of the top notch movies in 2008.

The worthless movie was 'The Day the Earth Stood Still' although I'd wish the whole movie could have stand as a still photograph of Keanu Reeves. I don't think we need to discuss a lot about that one.

The follow up movie was 'Gran Torino' is by none other than Mr.Clint Eastwood. Gran Torino has Mr.Eastwood himself as the main character 'Walt Kawoski' a former World War I soldier who operated in Korea. Kawoski is a very independent man who has a past which he likes to forget and a very lonely present time. At the start he projects himself to be a typical old age American who is so arrogant and big headed. His nerves are further tested by the fact that his neighborhood has ended up being a cross-cultural dump yard of people. The movie beautifully shows how the bond between him and the two young Hmong girl and boy builds up. As the movie unfolds the very same characteristics of Kawoski which you hated at the beginning starts to first entertain us and then even educate us about human relationships towards the end. Eastwood with his ever deepening voice of his cracks up some very good humour and also forces us to appreciate the fact that old style of living is indeed simpler & better.

I always judge a movie either by the number of 'Ohhhhhh' feelings or the number of feeling of some heavy chunk stuck up in the top of my throat. Being ‘Ohhh’d is an indication of being tricked by the movie which is good most of the time (Remember Usual Suspects/Memento). The second is the feelings we have towards the characters and their situation which indicates how well the movie has bonded us with the characters within 1 or 2 hours. Gran Torino left me up with a few heavy chunks stuck up in my throat (specially at the end) as an appreciation of how pure and worthy plain old style human relationships can be without all the religions and sciences trying to dissect and describe it.

I would recommend any of you to get a DVD and watch this.