Jump to page: 1 2 3
Thread overview
How to avoid throwing an exceptions for a built-in function?
May 10, 2017
k-five
May 10, 2017
Nicholas Wilson
May 10, 2017
Ivan Kazmenko
May 10, 2017
k-five
May 10, 2017
Stanislav Blinov
May 10, 2017
k-five
May 10, 2017
Stanislav Blinov
May 10, 2017
Moritz Maxeiner
May 11, 2017
k-five
May 13, 2017
Mike B Johnson
May 13, 2017
k-five
May 13, 2017
Stanislav Blinov
May 11, 2017
k-five
May 10, 2017
Ali Çehreli
May 11, 2017
crimaniak
May 11, 2017
k-five
May 11, 2017
H. S. Teoh
May 11, 2017
Jordan Wilson
May 12, 2017
k-five
May 12, 2017
k-five
May 12, 2017
Jonathan M Davis
May 12, 2017
k-five
May 12, 2017
ag0aep6g
May 12, 2017
H. S. Teoh
May 13, 2017
Jonathan M Davis
May 13, 2017
k-five
May 13, 2017
Bastiaan Veelo
May 10, 2017
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?

May 10, 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?

there has been talk of adding an overload that substitutes a supplied value on failure.
apart from that there is assumeWontThrow in the stdlib somewhere.
May 10, 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?

I assume that an empty string is a valid input then.
The question is, what value do you want `index` to have when the string is empty?
Maybe the old value, or some constant?
In any case, to better express your intent, you may write something like:

    if (user_apply[4] != "")
    {
        index = to !(int) (user_apply[4]);
    }
    else
    {
        index = ...;  // specify whatever your intent is
    }

This way, the code is self-documenting, and the program still throws when `user_apply[4]` is neither empty nor an integer, which may be the right thing to do in the long run.

Ivan Kazmenko.

May 10, 2017
On Wednesday, 10 May 2017 at 13:12:46 UTC, Ivan Kazmenko wrote:
> On Wednesday, 10 May 2017 at 12:40:41 UTC, k-five wrote:
----------------------------------------------------------
>
> I assume that an empty string is a valid input then.
> The question is, what value do you want `index` to have when the string is empty?
> Maybe the old value, or some constant?
> In any case, to better express your intent, you may write something like:
>
>     if (user_apply[4] != "")
>     {
>         index = to !(int) (user_apply[4]);
>     }
>     else
>     {
>         index = ...;  // specify whatever your intent is
>     }
>
> This way, the code is self-documenting, and the program still throws when `user_apply[4]` is neither empty nor an integer, which may be the right thing to do in the long run.
>
> Ivan Kazmenko.
----------------------------------------------------------
Thanks, but I know about what are you saying. The user_apply[4] has so many possibilities and I cannot use if-else
May 10, 2017
On Wednesday, 10 May 2017 at 13:27:17 UTC, k-five wrote:

> Thanks, but I know about what are you saying. The user_apply[4] has so many possibilities and I cannot use if-else

That doesn't sound right. Either you've already handled all the possible cases and thus expect the to! to not throw (can specify that i.e. via std.exception.assumeWontThrow), or, as you're saying, it's much more than an if-else, but in that case exception will be thrown on invalid input.
May 10, 2017
On Wednesday, 10 May 2017 at 14:27:46 UTC, Stanislav Blinov wrote:
> On Wednesday, 10 May 2017 at 13:27:17 UTC, k-five wrote:
>
>> Thanks, but I know about what are you saying. The user_apply[4] has so many possibilities and I cannot use if-else
>
> That doesn't sound right. Either you've already handled all the possible cases and thus expect the to! to not throw (can specify that i.e. via std.exception.assumeWontThrow), or, as you're saying, it's much more than an if-else, but in that case exception will be thrown on invalid input.

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

I know. But I am saying that I do not want to take care of any exceptions.
I just wanted to write:

int variable = to!int( string-type );

In fact something like using "no throw" is a function:
void init( ... ) nothrow {
    ...
    int variable = to!int( string-type );
    ...
}

but this is not valid.

Thanks anyway and I will test: Function std.exception.assumeWontThrow at this link:
https://dlang.org/library/std/exception/assume_wont_throw.html
May 10, 2017
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 wrote:
>> On Wednesday, 10 May 2017 at 13:27:17 UTC, k-five wrote:
>>
>>> Thanks, but I know about what are you saying. The user_apply[4] has so many possibilities and I cannot use if-else
>>
>> That doesn't sound right. Either you've already handled all the possible cases and thus expect the to! to not throw (can specify that i.e. via std.exception.assumeWontThrow), or, as you're saying, it's much more than an if-else, but in that case exception will be thrown on invalid input.
>
> ------------------------------------------------------------------
>
> I know. But I am saying that I do not want to take care of any exceptions.
> I just wanted to write:
>
> int variable = to!int( string-type );
>
> In fact something like using "no throw" is a function:
> void init( ... ) nothrow {
>     ...
>     int variable = to!int( string-type );
>     ...
> }
>
> but this is not valid.
>
> Thanks anyway and I will test: Function std.exception.assumeWontThrow at this link:
> https://dlang.org/library/std/exception/assume_wont_throw.html

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). If an exception is thrown, it'll propagate further up the stack, until you either handle it or abort the program. "nothrow" does not turn off exceptions, it simply forbids throwing them in the enclosing scope (i.e. calling anything that might throw is not allowed).
Or do you mean that you've already made sure that the user input is valid such that to!int() will never throw with it? In that case, yes, assumeWontThrow is a way to express that. But then, the only way you can be sure of that is if you've already parsed the input yourself, so I'm not clear on the intent.
Alternatively, if what you're calling still might throw but you don't want to deal with try/catch blocks, there's collectException function in std.exception that'll give you the exception if it was thrown and let you deal with it without using try/catch.
May 10, 2017
On Wednesday, 10 May 2017 at 21:19:21 UTC, Stanislav Blinov wrote:
> "nothrow" does not turn off exceptions, it simply forbids throwing them in the enclosing scope (i.e. calling anything that might throw is not allowed).

nothrow disallows the function scope to throw exceptions not derived from core.object.Error. It makes no claim about what happens *inside* the scope; you can throw as much as you want inside nothrow, as long as you take care to catch anything that's not a core.object.Error (which is what std.exception.assumeWontThrow essentially does).
May 11, 2017
On 5/10/17 3:40 PM, 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?

Use the "parse" family: https://dlang.org/phobos/std_conv.html#parse -- Andrei

May 10, 2017
On 05/10/2017 05:40 AM, 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
> }

Are you really fine with 'index' being left with its previous value? If so, you can write a function like the following:

void setMaybe(To, From)(ref To var, From from) {
    import std.conv : to, ConvException;
    try {
        var = to!To(from);
    } catch( ConvException conv_error ) {
    }
}

unittest {
    int index = 41;
    index.setMaybe("42");
    assert(index == 42);
    index.setMaybe("forty three");
    assert(index == 42);
    index.setMaybe("");
    assert(index == 42);
}

void main() {
}

If you want the variable to be set to a default value (perhaps .init), then here is an idea:

void setMaybe(To, From)(ref To var, From from, To def = To.init) {
    import std.conv : to, ConvException;
    try {
        var = to!To(from);
    } catch( ConvException conv_error ) {
        var = def;
    }
}

unittest {
    int index = 41;
    index.setMaybe("42");
    assert(index == 42);

    index.setMaybe("forty three");
    assert(index == 0);

    index.setMaybe(7);
    index.setMaybe("", 8);
    assert(index == 8);
}

void main() {
}

Ali

« First   ‹ Prev
1 2 3