May 11, 2017
On Wednesday, 10 May 2017 at 21:19:21 UTC, Stanislav Blinov wrote:
> On Wednesday, 10 May 2017 at 15:35:24 UTC, k-five wrote:
>> On Wednesday, 10 May 2017 at 14:27:46 UTC, Stanislav Blinov
---------------------------------------------------------------
> I don't understand. If you don't want to take care of exceptions, then you just don't do anything, simply call to!int(str).

Well I did that, but when the string is a valid type like: "10" there is no problems. But when the string is not valid, like: "abc", then to! function throws an exception.

Why I do not want to take care of that? Because I just need the value, if the string is valid, otherwise no matter what the value of string is.

First I just wrote:
index = to!int( user_apply[ 4 ] );

And this code is a part of a command-line program and the user may enter anything. So, for a valid string:
./program '10'   // okey

but for:
./program 'non-numerical' // throws an exception an 10 lines of error appear on the screen( console )

I just want to silent this exception. Of course it is useful for handling when someone wants to. But in my code I no need to handle it. So I want to silent that, without using try{}catch(){} block. I just wondered about try-catch and I want to know may there would be a better way instead of a dummy try-catch block.

Thanks for replying and mentioning. And I am sorry, since I an new in English Writing, if you got confuse.

May 11, 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.
May 11, 2017
On Wednesday, 10 May 2017 at 12:40:41 UTC, k-five wrote:
> I have a line of code that uses "to" function in std.conv for a purpose like:
>
> int index = to!int( user_apply[ 4 ] ); // string to int
>
> When the user_apply[ 4 ] has value, there is no problem; but when it is empty: ""
> it throws an ConvException exception and I want to avoid this exception.
>
> currently I have to use a dummy catch:
> try{
>     index = to!int( user_apply[ 4 ] );
> } catch( ConvException conv_error ){
>     // nothing
> }
>
> I no need to handle that, so is there any way to prevent this exception?

try this:
https://dlang.org/phobos/std_exception.html#ifThrown
May 11, 2017
On Thursday, 11 May 2017 at 17:18:37 UTC, crimaniak wrote:
> On Wednesday, 10 May 2017 at 12:40:41 UTC, k-five wrote:
---------------------------------------------------------
> try this:
> https://dlang.org/phobos/std_exception.html#ifThrown



Worked. Thanks.

import std.stdio;
import std.conv: to;
import std.exception: ifThrown;

void main( string[] args ){
	
	string str = "string";
	int index = to!int( str ).ifThrown( 0 ); // if an exception was thrown, it is ignored and then return ( 0 );
	writeln( "index: ", index );	// 0
}


May 11, 2017
On Thu, May 11, 2017 at 05:55:03PM +0000, k-five via Digitalmars-d-learn wrote:
> On Thursday, 11 May 2017 at 17:18:37 UTC, crimaniak wrote:
> > On Wednesday, 10 May 2017 at 12:40:41 UTC, k-five wrote:
> ---------------------------------------------------------
> > try this: https://dlang.org/phobos/std_exception.html#ifThrown
> 
> 
> 
> Worked. Thanks.
> 
> import std.stdio;
> import std.conv: to;
> import std.exception: ifThrown;
> 
> void main( string[] args ){
> 
> 	string str = "string";
> 	int index = to!int( str ).ifThrown( 0 ); // if an exception was thrown, it
> is ignored and then return ( 0 );
> 	writeln( "index: ", index );	// 0
> }

Keep in mind, though, that you should not do this in an inner loop if you care about performance, as throwing / catching exceptions will incur a performance hit.  Outside of inner loops, though, it probably doesn't matter.


T

-- 
Ph.D. = Permanent head Damage
May 11, 2017
On Thursday, 11 May 2017 at 18:07:47 UTC, H. S. Teoh wrote:
> On Thu, May 11, 2017 at 05:55:03PM +0000, k-five via Digitalmars-d-learn wrote:
>> On Thursday, 11 May 2017 at 17:18:37 UTC, crimaniak wrote:
>> > On Wednesday, 10 May 2017 at 12:40:41 UTC, k-five wrote:
>> ---------------------------------------------------------
>> > try this: https://dlang.org/phobos/std_exception.html#ifThrown
>> 
>> 
>> 
>> Worked. Thanks.
>> 
>> import std.stdio;
>> import std.conv: to;
>> import std.exception: ifThrown;
>> 
>> void main( string[] args ){
>> 
>> 	string str = "string";
>> 	int index = to!int( str ).ifThrown( 0 ); // if an exception was thrown, it
>> is ignored and then return ( 0 );
>> 	writeln( "index: ", index );	// 0
>> }
>
> Keep in mind, though, that you should not do this in an inner loop if you care about performance, as throwing / catching exceptions will incur a performance hit.  Outside of inner loops, though, it probably doesn't matter.
>
>
> T

This reason is why I sometimes use isNumeric if I have heaps of strings I need to convert,  to reduce exceptions. So something like:
int index = (str.isNumeric) ? to!int(str).ifThrown(0) : 0;

Jordan
May 12, 2017
On Thursday, 11 May 2017 at 19:59:55 UTC, Jordan Wilson wrote:
> On Thursday, 11 May 2017 at 18:07:47 UTC, H. S. Teoh wrote:
>> On Thu, May 11, 2017 at 05:55:03PM +0000, k-five via Digitalmars-d-learn wrote:
>>> On Thursday, 11 May 2017 at 17:18:37 UTC, crimaniak wrote:
>>> > On Wednesday, 10 May 2017 at 12:40:41 UTC, k-five wrote:
>>> ---------------------------------------------------------
>
> This reason is why I sometimes use isNumeric if I have heaps of strings I need to convert,  to reduce exceptions. So something like:
> int index = (str.isNumeric) ? to!int(str).ifThrown(0) : 0;
>
> Jordan
--------------------------------------------------------------
Interesting! I was worried about performance and for that I did not want to use try-catch.
So (isNumberic) is a good solution.
http://dlang.org/phobos/std_traits.html#isNumeric


May 12, 2017
On Friday, 12 May 2017 at 08:32:03 UTC, k-five wrote:
> On Thursday, 11 May 2017 at 19:59:55 UTC, Jordan Wilson wrote:
>> On Thursday, 11 May 2017 at 18:07:47 UTC, H. S. Teoh wrote:
>>> On Thu, May 11, 2017 at 05:55:03PM +0000, k-five via Digitalmars-d-learn wrote:
>>>> On Thursday, 11 May 2017 at 17:18:37 UTC, crimaniak wrote:
>>>> > On Wednesday, 10 May 2017 at 12:40:41 UTC, k-five wrote:
>>>> ---------------------------------------------------------
>>
>> This reason is why I sometimes use isNumeric if I have heaps of strings I need to convert,  to reduce exceptions. So something like:
>> int index = (str.isNumeric) ? to!int(str).ifThrown(0) : 0;
>>
>> Jordan
> --------------------------------------------------------------
> Interesting! I was worried about performance and for that I did not want to use try-catch.
> So (isNumberic) is a good solution.
> http://dlang.org/phobos/std_traits.html#isNumeric
----------------------------------------------------------------

NOT a GOOD solution! Since it accept type and NOT variable:

so:

index = (str.isNumeric) ? to!int(str).ifThrown(0) : 0;

is:

temp.d(11): Error: template std.traits.isNumeric cannot deduce function from argument types !()(string), candidates are:
May 12, 2017
On Friday, May 12, 2017 08:32:03 k-five via Digitalmars-d-learn wrote:
> On Thursday, 11 May 2017 at 19:59:55 UTC, Jordan Wilson wrote:
> > On Thursday, 11 May 2017 at 18:07:47 UTC, H. S. Teoh wrote:
> >> On Thu, May 11, 2017 at 05:55:03PM +0000, k-five via
> >>
> >> Digitalmars-d-learn wrote:
> >>> On Thursday, 11 May 2017 at 17:18:37 UTC, crimaniak wrote:
> >>> > On Wednesday, 10 May 2017 at 12:40:41 UTC, k-five wrote:
> >>> ---------------------------------------------------------
> >
> > This reason is why I sometimes use isNumeric if I have heaps of
> > strings I need to convert,  to reduce exceptions. So something
> > like:
> > int index = (str.isNumeric) ? to!int(str).ifThrown(0) : 0;
> >
> > Jordan
>
> --------------------------------------------------------------
> Interesting! I was worried about performance and for that I did
> not want to use try-catch.
> So (isNumberic) is a good solution.
> http://dlang.org/phobos/std_traits.html#isNumeric

That's the wrong isNumeric. Unfortunately, both std.string and std.traits have an isNumeric. std.traits.isNumeric is an eponymous template that tests whether a type is an integral or floating point type, whereas std.string.isNumeric is a templated function which tests whether a string (or other range of characters) represents an integer or floating point literal. So, std.traits.isNumeric won't work at all for what you want, and std.string.isNumeric will sort of do what you want - the main caveat being that it will also accept floating point values, whereas to!int will not.

So, if you have

auto i = str.isNumeric ? to!int(str).ifThrown(0) : 0;

then it will work but will still throw (and then be caught by ifThrown) if str is a floating point value. Alternatively, you could check something like

auto i = str.all!isDigit ? to!int(str).ifThrown(0) : 0;

(where all is in std.algorithm and isDigit is in std.ascii), and that _almost_ wouldn't need ifThrown, letting you do

auto i = str.all!isDigit ? to!int(str) : 0;

except that str could be an integral value that wouldn't fit in an int, in which case to!int would throw. But std.string.isNumeric will work so long as you're willing to have have an exception be thrown and caught if the string is a floating point literal.

- Jonathan M Davis

May 12, 2017
On Friday, 12 May 2017 at 09:03:39 UTC, Jonathan M Davis wrote:
> That's the wrong isNumeric. Unfortunately, both std.string and std.traits have an isNumeric. std.traits.isNumeric is an eponymous template that tests whether a type is an integral or floating point type, whereas std.string.isNumeric is a templated function which tests whether a string (or other range of characters) represents an integer or floating point literal. So, std.traits.isNumeric won't work at all for what you want, and std.string.isNumeric will sort of do what you want - the main caveat being that it will also accept floating point values, whereas to!int will not.
>
> So, if you have
>
> auto i = str.isNumeric ? to!int(str).ifThrown(0) : 0;
>
> then it will work but will still throw (and then be caught by ifThrown) if str is a floating point value. Alternatively, you could check something like
>
> auto i = str.all!isDigit ? to!int(str).ifThrown(0) : 0;
>
> (where all is in std.algorithm and isDigit is in std.ascii), and that _almost_ wouldn't need ifThrown, letting you do
>
> auto i = str.all!isDigit ? to!int(str) : 0;
>
> except that str could be an integral value that wouldn't fit in an int, in which case to!int would throw. But std.string.isNumeric will work so long as you're willing to have have an exception be thrown and caught if the string is a floating point literal.
>
> - Jonathan M Davis

-----------------------------------------------------------------

Thank you for mentioning it. Since I had seen D-conference 2016 in YouTube and it talked about isNumeric in std.traits, then I used std.traits. and does not worked as I wanted.

But std.string: isNumeric, worked.

string str = "string";
int index = str.isNumeric ? to!int( str ) : 0;
writeln( "index: ", index ); // 0 and without throwing any exceptions