I am working on a library designed to communicate (over RS232 serial communication) with external devices. I was thinking about error handling strategy and exceptions seemed to be right and industry standard way of reporting errors.
So I read few guidelines on exceptions. One pretty clearly states that I should not worry about performance hit:
Do not use error codes because of concerns that exceptions might affect performance negatively.
Other told me NOT to throw exception in normal cases:
Do not use exceptions for normal or expected errors, or for normal flow of control.
I am not able to draw clear line between normal/expected and other cases. For example in my library, a operation may fail because:
I can think all above as expected problems because they can happen in practice very often (infact many marketing morons call me to solve the ^problem^ in my software only to find out they didnt connect the cable to their laptop). So may be exceptions should not be thrown because otherwise application programmer will have to catch those at plenty of places (a lot of catch blocks are also NOT nice to have I believe).
On the other hand, I also tend to think that these are all errors which I somehow need to report to application programmer, and exception seems to be the way to do that. If I don't use exceptions, I will need to report these problems using some error code or error enums. (ugly, I know).
Which approach do you think I should take?
As a general rule of thumb, exception usage should balance the following two opposing goals: Exceptions should not be the norm. They involve the creation of an additional object, so, if only from a performance standpoint, it is problematic if exceptions can occur frequently. Mixing data and control should be avoided.
An exception should be thrown when a function experiences a failure, i.e., an error. A function is a unit of work, and failures should be viewed as errors or otherwise based on their impact on functions.
It is important to understand how to throw exceptions in Java. This will allow you to create higher quality code where errors are checked at compile time instead of runtime, and create custom exceptions that make debugging and recovery easier.
You are developing a library, a component that will be utilized by other applications.
Therefore in the expected cases you mention I would certainly use exceptions to communicate to the calling application that something is amiss. You should define a custom exception for each of these scenarios and then clearly document when they may occur.
This then allows the application client code to make the decision as to how best to proceed. Only the client application can make this decision and clearly documented exceptions greatly aid this.
The best thing about a custom exception is that you can provide multiple meaningful / useful pieces of data relating to the problem/exception. This data is also nicely encapsulated in one object. Compare this to error codes and return values.
Performance can be an issue but only when the exception is thrown within a tight loop or some other high activity situation. To avoid this you could also apply a pattern used by the .NET Framework, namely provide Try....() methods (eg TryParse()
) that return boolean indicating if an action succeeded or failed.
Either way I would go with custom exceptions initially, and then do performance testing to actually see if there are certain parts of the library that may need optimization.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With