News for hackers. Stuff that matters.
Re:Everything about Java 8 by MichaelGG on Wednesday March 27, 2013 @01:30PM (#5450762)
TryParse returns a boolean. So you use it like this:

  int res;
  if int.TryParse(s, out res) { // OK } { else // not ok }
You certainly do not need an exception to deal with the simple case of "did this string parse into an int".

Edit: A great alternative signature is to use Maybe/Option, so you get Some int or None.

  match int.TryParse s with
  | None   -> ...
  | Some i -> ...
Re:Everything about Java 8 by DoubleMalt on Wednesday March 27, 2013 @02:21PM (#5451102)
Ah apparently I remembered it wrong. I thought it was

int res = int.TryParse(s, 0); // 0 as the default if parsing fails

Of course if there is no reasonable default you should bubble the thing up anyway or handle it on the spot. But very often for input parsing there is sane default you can choose.

Re:Everything about Java 8 by lucastheisen on Wednesday March 27, 2013 @05:34PM (#5452137)
is that really more work than:

  int res;
  try { res = Integer.parseInt( s ); } // if part
  catch ( NumberFormatException nfe ) { } // else part
The words are different (try/catch instead of if/else), but still 2 blocks of code with similar syntax...
Re:Everything about Java 8 by MichaelGG on Wednesday March 27, 2013 @08:35PM (#5452792)
The main reason for TryParse, AFAIK, is avoiding the massive expense of exceptions on the CLR.

Another reason is composability. If you're checking several values together, returning a Maybe/Option is much more clear and usable.