Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • 1
    +1, good points made. I think that some processing may be valid in the catch such as preparation of an error message and error logging. Freeing up of expensive resources such as db connections and in (.NET) COM references could be added to the Finally block. Commented Apr 25, 2012 at 12:34
  • @EmmadKareem I thought about freeing resources, but usually you have to free those anyway at some point even without error situation. Thus you might repeat yourself. Preparing a log message is of course acceptable. Commented Apr 25, 2012 at 13:28
  • @MichaelBorgwardt I think the API design is not so poor (though it could be better). Trying to parse an invalid string is clearly an error condition and thus exactly the "widely accepted case" you mentioned. Agreed, a method to determine if a string is syntactically correct number comes in handy (this is where it could be better). However, forcing everybody to call this method before actual parsing is not possible and we need to handle the error. Mixing actual result and error code is uncomfortable and so you'd still throw the exception. But maybe a runtime exception. Commented Apr 25, 2012 at 13:50
  • 2
    @scarfridge: I agree completely with you :) The absence of a boolean-returning isInteger(String) method is all I meant with "poor API design" Commented Apr 25, 2012 at 14:21
  • -1: isInteger doesn't help. Instead of try { parse(s); ...; } catch { oops; } you have if (isInteger(s)) { parse(s); ...; } else { oops; }. Now the parsing happens twice. Worst of all would be for parse to return a special value instead of throwing. Commented Apr 25, 2012 at 19:16