Note that there are some explanatory texts on larger screens.

plurals
  1. POConfused about when to throw an exception
    primarykey
    data
    text
    <p>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.</p> <p>So I read few guidelines on exceptions. <a href="http://msdn.microsoft.com/en-us/library/ms229009.aspx" rel="nofollow noreferrer">One</a> pretty clearly states that I should <strong>not</strong> worry about performance hit:</p> <blockquote> <p>Do not use error codes because of concerns that exceptions might affect performance negatively.</p> </blockquote> <p><a href="http://msdn.microsoft.com/en-us/library/8ey5ey87(VS.71).aspx" rel="nofollow noreferrer">Other</a> told me NOT to throw exception in normal cases:</p> <blockquote> <p>Do not use exceptions for normal or expected errors, or for normal flow of control.</p> </blockquote> <p>I am not able to draw clear line between normal/expected and other cases. For example in my library, a operation may fail because:</p> <ol> <li>There is no response from device. (no cable connected, device not turned on, wrong baud rate)</li> <li>Operation request is rejected by device because it couldn't authenticate the request.</li> <li>Communication failed in between. (someone tripped over the cable, device was powered off suddenly).</li> </ol> <p>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).</p> <p>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).</p> <p>Which approach do you think I should take?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload