Software Engineer

I am a Software Engineer. I have a Bachelor (Honours) of Science in Information Technology from the University of Sunderland - Class of 2003. I have been developing software since 2001 when I was offered a role at CERN as part of their Technical Student Programme.

By 2016 I had grown really tired of the software industry and by the end of 2019 Apple killed whatever excitement I had left. I am not sure what the next 10 years will bring. What I do know is that my apettite to do work that is impactful has only grown bigger and stronger. Great people make me tick more than anything.

I am also tired.

Be Pissed, Be Polite. Always Say "Good Bye!" (the Reason Behind 'finally' In A Try:catch)

/**
 * You are asking for trouble here
 */
public void buyMuffin(LocalStore localStore, Flavour flavour)
{
    try
    {
        localStore.buyMuffin(flavour);
    }
    catch(MuffinNotFoundException e)
    {
        System.out.println("Pissed!");
    }
        
    System.out.println("Good bye!");
}

/**
 * Not my problem I don't have any money!
 * (InsufficientFundsException not handled)
 * You aren't being polite.
 */
public void buyMuffin(LocalStore localStore, Flavour flavour)
{
    try
    {
        localStore.buyMuffin(flavour);
    }
    catch(MuffinNotFoundException e)
    {
        System.out.println("Pissed!");
    }

    System.out.println("Good bye!");
}

/**
 * I don't give a damn! Still have to be polite though.
 */
public void buyMuffin(LocalStore localStore, Flavour flavour)
{
    try
    {
        localStore.buyMuffin(flavour);
    }
    catch(MuffinNotFoundException e)
    {
        System.out.println("Pissed!");
    }
    finally
    {
        System.out.println("Good bye!");
    }
}

/**
 * Just want my muffin
 */
public void buyMuffin(LocalStore localStore, Flavour flavour)
{
    try
    {
        localStore.buyMuffin(flavour);
    }
    finally
    {
        System.out.println("Good bye!");
    }
}

There has been a lot of debate about exceptions and their usage in the Java world. This post is not about it.

A foreword on what exceptions really are in case you are still confused.

Consider yourself going to your local store to buy a muffin (an addict here).

Now I prefer a chocolate one but the local store might not have one. So I walk in, go to the muffins section and… there is none! Something unexpected happened (hint an Exception). Yeah I’m pissed but I still have to be polite and say goodbye on my way out.

Now the only way to ensure that; is the ‘finally’ clause in a try/catch block. Here is why.

Without a ‘finally’ this might very well work. You do handle the exception right there and you still get to say “Good bye!” at the end. There are 2 things to consider though.

  • the LocalStore#buyMuffin(Flavour) method might very well change (especially if it’s third party code) introducing a new exception to the mix (InsufficientFundsException since people were just buying muffins without having enough money to pay). You aren’t being polite anymore.
  • you may not be responsible for handling the situation of not having enough money to buy a muffin. Your dad/employer/clients are. (in the “real world” an example would be to have a common class to log the exceptions)

Now in both cases not having a finally clause will simply break your code sooner or later. (Picture this with database connections and you have a serious leak there)

Disclaimer Take the advice. Not the code. It’s just an example.