How to avoid throwing an exceptions for a built-in function?

k-five via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 11 09:27:13 PDT 2017


On Wednesday, 10 May 2017 at 21:44:32 UTC, Andrei Alexandrescu 
wrote:
> On 5/10/17 3:40 PM, k-five wrote:
-----------------------------------
>> I no need to handle that, so is there any way to prevent this 
>> exception?
>
> Use the "parse" family: 
> https://dlang.org/phobos/std_conv.html#parse -- Andrei
-----------------------------------

This is my answer :). I want a way to covert a string without 
facing any exceptions.

But may I do not understand so well the documentation
It says:
The parse family of functions works quite like the to family, 
except that:

     1 - It only works with character ranges as input.
     2 - It takes the input by reference. (This means that rvalues 
- such as string literals - are not accepted: use to instead.)
     3 - It advances the input to the position following the 
conversion.
     4 - It does not throw if it could not convert the entire 
input.

here, number 4: It does not throw if it could not convert the 
entire input.

then it says:
Throws:
A ConvException if the range does not represent a bool.

Well it says different things about throwing!

Also I tested this:

import std.stdio;
import std.conv: parse;

void main( string[] args ){
	
	string str = "string";
	int index = parse!int( str );
	writeln( "index: ", index );
}

the output:
std.conv.ConvException@/usr/include/dmd/phobos/std/conv.d(2111): 
Unexpected 's' when converting from type string to type int
and so on ...

Please correct me if I am wrong.


More information about the Digitalmars-d-learn mailing list