Throwing exceptions - don’t throw yourself away as wellFiled Under: Weekly Tuesday Dose of goodness
In this article, we’re going to talk about Exceptions.
In the major 3GL programming languages, such as C++, Java and C#, all of them has exception handling mechanisms.
Yet a lot of developers are grossly misunderstood on their nature, when we’re supposed to use them and how do handle them.
How is that so? Read on….
What are exceptions?
In simple terms, it simply means, something unexpected.
This is contradicts what a normal program is supposed to be; predictable and deterministic. So why does a program become unpredictable or non-deterministic?
This is due to the advent of certain technologies. One of the biggest culprit would be threads. Culprit as it may be, it’s a necessary evil to keep things running at optimal levels.
To note, it’s just one of the many culprits. So what are the others? Let me list a few general or abstract examples:
1) Accessing external devices
2) Accessing things you have no direct or immediate control over
3) Dynamic access of certain things which you cannot expect or predict
Let’s talk about these 3 general examples in detail:
1) Accessing external devices
What do we mean by external devices? Let’s say, you’re reading from a thumb drive or removeable media. What makes you think that they cannot be removed in the middle of I/O operations?
Thus should it be removed unexpectedly, an IO Exception would be thrown isn’t it? So that you can handle the exception scenario in any case that the data cannot be read or written.
Handling such a scenario with a try-catch code block is perfectly normal and correct.
2) Accessing things you have no direct or immediate control over
This occurs when your code interfaces with operations that depend on timings. Should you miss that timing, there might be dire consequences. Therefore there’re cases where if a timing is not met, a time out exception is thrown.
This could be due to orchestration efforts and synchronization. That’s why in some games, players eventually get dropped after a few time outs.
3) Dynamic access of certain things which you cannot expect or predict
When it comes to dynamic access, there can be a few things that can happen. On the low level side of things, you can get a code miscall due to the code misalignment in the dynamic-linked library (DLL) for example.
You can’t control it once you’ve run it because the codes are already compiled before hand and you have limited knowledge (ie, via interfaces) of what you’re going to expect.
You can also get invalid values during runtime which you’re not expected. That’s why you can get Illegal Argument exception and so on.
Lastly, since we write programs that’s supposed to work on multiple systems, don’t expect everybody’s memory to be of the same size! What happens when you run out of memory?
Conclusion
If you’re expecting something to go wrong, use a return value to represent the status of the call. Native types such as bool, short and int make excellent return value types and they don’t cost a thing to implement. (Negligible performance penalty)
If you’re expecting something to be unpredictable, handle it with a try-catch block instead. You can do everything in your power to make things as deterministic as possible. As for those things that you really really cannot control, try-catch it - there’s no shame.
Lastly, do not abuse exceptions. Do not try-catch a simple deterministic function call that throws a stupid illegal arguments exception just because you pass in a 1 instead of 2. That’s stupid. Rewinding the stack unnecessarily can decrease performance significantly.
I’ve seen too many cases of exception-handling abuses or lack of handling for the right exception scenarios. Therefore, I hope that after reading this article, you’ll be more aware of what to do or when to do with exceptions.
Signing off,
Jeremy
- Permalink
- Admin
- 30 Jun 2009 10:42 AM
- Comments (0)