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*

16
  • 4
    I think this is actually a pretty good argument. Your code is clean and concise and makes sense in what it is doing. This is actually similar to what is happening in the code I am seeing, only it is much more complex, is nested and spans 30+ lines of code. Commented Sep 13, 2011 at 11:40
  • 1
    One could argue that Java could really use something more like C#'s TryParse. In C# that code would be int i; if(long.TryParse(s, out i)) return i; else return null;. TryParse returns false if the string could not be parsed. If it could be parsed it sets the output argument to the results of parsing and return true. No exceptions occur, (even internally in TryParse) and especially no exceptions that of a type that mean programmer error, like .NETs FormatException always indicates. Commented Sep 13, 2011 at 17:06
  • 1
    @Kevin Cathcart, but why is that better? the code that catches the NumberFormatException is much more obvious to me then checking the result of TryParse for a null. Commented Sep 13, 2011 at 18:28
  • 1
    @ChrisMiskowiec, I suspect which one you find clearer is pretty much a matter of what you are used to. I find catching the exception much easier to follow then checking for magic values. But that's all subjective. Objectively, I think we should prefer exceptions because they have a sane default (crash the program), rather then a default which hides bug (continue on as if nothing has happened). It is true that .NET performs poorly with exceptions. But thats a result of .NET's design, not the nature of exceptions. If on .NET, yes you've got to do that. But in principle, exceptions are better. Commented Oct 23, 2012 at 22:03
  • 1
    "It doesn't matter how often these exceptions are thrown until there is a demonstrated performance problem." So you just keep on adding code you know is bad until your app actually starts to stop functioning, then you have to go through it and change possibly hundreds (thousands) of lines of code? This is insanity! Commented Mar 11, 2021 at 4:00