Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused about when to throw an exception

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:

  1. There is no response from device. (no cable connected, device not turned on, wrong baud rate)
  2. Operation request is rejected by device because it couldn't authenticate the request.
  3. Communication failed in between. (someone tripped over the cable, device was powered off suddenly).

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?

like image 540
Hemant Avatar asked Sep 19 '09 05:09

Hemant


People also ask

When should we throw an exception?

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.

How do you decide if an exception should be thrown in a method?

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.

Why do we need to throw an exception?

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.


1 Answers

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.

like image 119
Ash Avatar answered Oct 24 '22 10:10

Ash