A question I was asked recently posed an interesting question about exceptions and the cost of using them as a general error handling mechanism.
Xojo uses whats know as “zero cost exception handling”. Essentially at runtime there is no penalty for having exception handling in place. It imposes no runtime overhead when NO exceptions are encountered. But, when exceptions are encountered, it can be “expensive”.
Expensive in this sense can mean it induces slowness or requires more memory. Or both.
So I put together a simple example that demonstrates the cost of using exception handling instead of error codes. Its very simple and uses a deprecated API that reported error codes instead of the newer one that raised an exception instead.
Its just a simple desktop application with a textarea on the default window and this code in the Window’s Open event
Dim f As folderitem = SpecialFolder.Desktop.Child("foo")
Dim errorcodeStart As Double = Microseconds
Dim errorCodeCount As Integer
For i As Integer = 1 To 1000
Dim ts As TextInputStream = f.OpenasTextFile
If f.LastErrorCode <> 0 Then
errorCodeCount = errorCodeCount + 1
End If
Next
Dim errorcodeEnd As Double = Microseconds
Dim exceptionStart As Double = Microseconds
Dim exceptionCount As Integer
For i As Integer = 1 To 1000
Try
Dim ts As TextInputStream = TextInputStream.Open(f)
Catch IOX As IOException
exceptionCount = exceptionCount + 1
End Try
Next
Dim exceptionEnd As Double = Microseconds
Dim errorTotal As Double = errorcodeEnd - errorcodeStart
Dim exceptionTotal As Double = exceptionEnd - exceptionStart
TextArea1.AppendText "1000 iterations" + EndOfLine
TextArea1.AppendText "Error Code = " + Str(errorTotal) + "ms" + EndOfLine
TextArea1.AppendText "Exceptions = " + Str(exceptionTotal) + "ms" + EndOfLine
In a debug run on my compute I get the following output.
1000 iterations
Error Code = 28096.87ms
Exceptions = 41711.62ms
The version using exceptions is juts about 50% slower over 1000 iterations when debugging.
In a version compiled with the DEFAULT setting the difference is less, but still present.
1000 iterations
Error Code = 28367.42ms
Exceptions = 35925.94ms
Exceptions are still about 25% slower than error codes. The Moderate optimization setting is similar
1000 iterations
Error Code = 29292.96ms
Exceptions = 34415.9ms
Aggressive settings remain similar
1000 iterations
Error Code = 28222.49ms
Exceptions = 35309.52ms
There are some advantages to exceptions. Unlike error codes they are impossible to ignore at runtime. At some point you MUST put code in place to handle them or your application will just quit with an unhandled exception error.
However, there’s nothing in Xojo that helps you make sure you have handled the possible exceptions that can be raised, and also nothing that tells you what exceptions might be raised.
So heads up before you dive into using exceptions everywhere as a general error handling mechanism in your applications. There are costs to doing this and they could manifest themselves in slower code or code that requires more memory. Or both.