int.parse and double.parse error question

802 views
Skip to first unread message

Shailen Tuli

unread,
Mar 22, 2013, 8:00:29 AM3/22/13
to mi...@dartlang.org
Both int.parse and double.parse come with internal error handling options.


external static int parse(String source, { int radix, int onError(String source) });
external static double parse(String source, [double handleError(String source)]);

Can anyone point me to a practical example for error handling within these methods?

- Shailen Tuli

Yohan Beschi

unread,
Mar 22, 2013, 8:07:14 AM3/22/13
to mi...@dartlang.org
If the string can't be parsed, instead of using a try/catch you can use a function handling the error and returning a default value.



- Shailen Tuli

--
Consider asking HOWTO questions at Stack Overflow: http://stackoverflow.com/tags/dart
 
 

Alex Tatumizer

unread,
Mar 22, 2013, 9:48:22 AM3/22/13
to mi...@dartlang.org
This "onError" callback would be much more useful if system could pass the following parameters:
string being parsed 
position where error was detected
value that would be returned if string ended at error position

Then I would at least know some use case. Right now, what's the point? Ignoring user's input and substituting your own? BTW, calling it a "default value" is a joke, right?


 
 

Shailen Tuli

unread,
Mar 22, 2013, 11:33:20 AM3/22/13
to mi...@dartlang.org
The string being passed *is* passed to the onError callback. I'm just not sure what I can do with it. Check to see if it has illegal characters and throw a FormatException with a better error message than the default? Honestly, I can't imagine using this in real code.

Also, I wish the int.parse() and double.parse() used consistent signatures.  The error handler is a named param in int.parse and a positional parameter in double.parse.  

James Wendel

unread,
Mar 22, 2013, 12:20:26 PM3/22/13
to mi...@dartlang.org
Just for reference:  http://api.dartlang.org/docs/releases/latest/dart_core/int.html#parse

Method signature:
int parse(String source, {int radix, int onError(String source)})

Example:

  

print(int.parse("42", onError: (s) => 0));

James Wendel

unread,
Mar 22, 2013, 12:32:02 PM3/22/13
to mi...@dartlang.org
Shailen: I have submitted a bug to this effect (making int.parse and double.parse signatures more similar):

Ladislav Thon

unread,
Mar 22, 2013, 12:46:04 PM3/22/13
to mi...@dartlang.org
Shailen: I have submitted a bug to this effect (making int.parse and double.parse signatures more similar):

This should definitely be fixed. Starred! :-)

LT
Reply all
Reply to author
Forward
0 new messages