On Exceptions

Programmers are generally aware of two primary methods of exception handling. You have the old C-style error code method:

int hMissileControl;
int iReturnCode = launchMissiles(&hMissileControl);
if (iReturnCode == ACCIDENTALLY_HIT_INNOCENTS)
    printf("My bad.");
else
    controlMissile(hMissileControl);

You also have the more common throw/raise paradigm:

MissileController missileController;
try {
    missileController = launchMissiles();
    controlMissile(missileController);
} catch (AccidentallyHitInnocentsException)
    printf("My bad.");

This is the predominant paradigm in popular languages like C++, Java, Python, and Ruby.

I’ve heard complaints from both sides. The C-stylers posit, “throwing exceptions introduces all sorts of hidden, complex return paths in your program!” They’re right. With exceptions, a simple program like the following cannot be guaranteed to be simple. There is much more than meets the eye.

void main() {
    doSomethingSimple();  // this might throw
}

Who knows what doSomethingSimple might throw? When does it throw? Can you guarantee it won’t throw with certain inputs?

Furthermore, there’s the classic edition of the hated “blind man’s catch”:

try
    somethingComplicated();
catch (...)
    printf("Something horrible happened...no idea what.");

Polymorphic exception handling opens the door to horrendous laziness like the sample above. Programmers can simply wrap code in a huge sarcophagus-sized band-aid and hope everything turns out alright. Of course, programmers ought to only catch the exceptions they know they can handle, but instead of doing all that research, they figure catching all exceptions under heaven will suffice.

But the C-style method comes with its own catch (pun intended). Exception throwers rightly argue that error codes have a potentially worse side-effect: code is innocent until proven guilty. Now, in our democracy, “innocent until proven guilty” is a jolly good thing to have. But in programming languages, it’s nigh deadly. What I mean is this: in a C-style program, you ignore errors by doing nothing at all. If you want to know an error happened, you have to explicitly check your return codes. But in a throw-style program, the only way to ignore an error is to explicitly catch it. Blindly ignoring errors is a deadly thing to do in programming, and that’s what C-style programming gives you by default.

Take these two programs for example:

// C-style
initializeStuff();  // this returns an error code if it fails
doTheRest();

// throw-style
initializeStuff();  // this throws if it fails
doTheRest();

Do you see the difference? The code is identical. But one of these programs will blindly doTheRest even if the initialization failed. Then when doTheRest explodes miserably, the programmer has to weed through debug logs (if they’re lucky) or use a debugger (if they’re luckier) to find out what happened. But the other program dies instantly with a clean stack trace describing what happened.

So which is better, C-style or throw-style? I don’t know. There have probably been studies to answer this question, but I haven’t read them.

Fortunately, there’s a third way that has the best of both worlds. It doesn’t introduce all sorts of hidden return paths through your code, but it also doesn’t let you blindly keep chugging along when your engine just blew. Because Haskell does such a elegant job of achieving this third way, I’ll use it as an example.

Let’s use sqrt (square root function) as an example. The sqrt function takes integers as input and returns a floating point number as its result. In Haskell that would look like this:

sqrt :: Int -> Double  -- what do we do on negative ints?

The only problem is that sqrt doesn’t work on negative numbers (unless you handle complex numbers, but let’s pretend you don’t feel like doing that right now). So when sqrt is handed a negative number, what do you do? The C-stylers would say that you need to return an error code (and unkindly force the output to a pointer-ridden input argument). The throwers would say to throw. But this third way does neither. Look at the new type signature:

sqrt :: Int -> Maybe Double  -- I might work, I might not, depending on what you give me.

This new type signature means that sqrt might return a Double-floating point number, or it might return Nothing.

Instead of throwing, Haskell’s type system lets you wrap your return value in a thing called a Maybe. This Maybe thing basically tells the compiler that the value will either be a Double or Nothing.

Now, if you are doing normal things with your sqrt function, everything will work jolly well. But if you accidentally give it -453, it will return Nothing and everything will halt. Of course, like the throwing paradigm, you can, if you want, handle this Nothing case and keep on moving. But, by default, your program will not ignore the problem.

This gives you the best of both worlds. Your functions only have one return path, but your program can’t blindly keep trucking if something goes horribly wrong.

Haskell has other types like Maybe that allow functions to return more than just Nothing, perhaps an error description or an error code.

Haskell’s common method of error handling isn’t confined to just Haskell, though. With a little OOP magic, it’s easy to make functions in C++ that have similar behavior. But Haskell’s system is especially good at it, which is why it’s so common.

Maybe the next time you want to handle errors, you’ll consider Maybe.


How do you personally walk in the Spirit?

“Walk by the Spirit, and you will not gratify the desires of the flesh” (Galatians 5:16) said the Apostle Paul. The Scriptural command is echoed repeatedly: “walk by the Spirit” (Galatians 5:16), “live according to the Spirit” (Romans 8:5), and “set the mind on the Spirit” (Romans 8:6). Since those words were penned, it has


How does the Biblical teaching of Hell impact you?

In the New Testament, “hell” is the translator’s word of choice for three different Greek terms: gehenna (Strong’s G1067), tartaroo (Strong’s G5020), and hades (Strong’s G86). It is by means of these words that the Lord God communicates the most sobering and terrifying truth in the Bible. All three terms refer to a place where


Worthy of Praise – from Philippians 4:8

“Finally brethren, if there is anything worthy of praise…dwell on these things.” — Philippians 4:8 (ESV) The culmination of all these attributes of Jesus is also the culmination of the believer’s meditation: worship. Jesus wants our meditation to always end in worship. And why shouldn’t it? We have seen more than enough to solicit our


Any Excellence – from Philippians 4:8

“Finally brethren, if there is any excellence…dwell on these things.” — Philippians 4:8 (ESV) Unlike the words Paul has used so far, excellence is not an attribute in itself. Rather, it is a degree of quality. Here Jesus is reminding us not just of his own qualities, but of the high and excellent degree of


Commendable – from Philippians 4:8

“Finally brethren, whatever is commendable…dwell on these things.” — Philippians 4:8 (ESV) The Greek word here for commendable (or good report, or good repute, or admirable in the other translations) only occurs in the New Testament once, here in this verse. It refers to things spoken in a kind spirit and with good-will toward others.


Lovely – from Philippians 4:8

“Finally brethren, whatever is lovely…dwell on these things.” — Philippians 4:8 (ESV) Loveliness describes yet another facet of God’s person. But loveliness cannot be separated from truth, honor, rightness, or purity. It is an attribute that can only be possessed by someone who has all the others too. God wants us to dwell on things


Pure – from Philippians 4:8

“Finally brethren, whatever is pure…dwell on these things.” — Philippians 4:8 (ESV) Pure things are immaculate, chaste, clean, and free from defect or corruption. God is pure, and he cannot be corrupted (1 John 3:3). But man stands in stark contrast to God’s purity. Though made pure and innocent, we have fallen far from our original


Right – from Philippians 4:8

“Finally brethren, whatever is right…dwell on these things.” — Philippians 4:8 (ESV) Right things (or just things in the KJV) are those that have the approval of God. They comply with his desires and they are in accordance with his commands. Throughout the Bible, we learn that “the precepts of the Lord are right” (Psalm 19:8),


Honorable – from Philippians 4:8

“Finally brethren, whatever is honorable…dwell on these things.” — Philippians 4:8 (ESV) Again, the Lord Jesus wants his people to meditate on things that are honorable, or honest (KJV). These are the things, actions, or people who are deserving of respect. Honorable things don’t appear out of nowhere. They earn their title. They are untarnished and