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

Mike B Johnson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 12 19:40:17 PDT 2017


On Thursday, 11 May 2017 at 16:07:22 UTC, k-five wrote:
> 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.

You are not making a lot of sense:

1. Exception do bubble up, so you don't need to "handle" 
exceptions at the call site if you don't want to. The whole point 
of exceptions is do effectively do what you want.

2. You say that you don't have to deal with it in your code but 
you are trying to deal with it now.


You are not clear on exactly what you are trying to accomplish.

Can you be more specific on why you do not want to add a 
try/catch/if-else/ifThrown/etc? Is it because you don't need to 
handle it in your own usage? If that is true, will others ever 
use it?

Basically all you have to do is ask yourself this:

"Do I need to EVER handle the case where the data is invalid?"

If you do(e.g., other users will use your code) then you better 
implement some type of exception handling. Else all you have to 
do is always put in the right data(not good because it might bite 
you in the ass one day).

If you just want less noise when the program crashes, just put a 
try/catch block in main and catch all generic exceptions and exit 
with a simple error message like "There was an ERROR, I do not 
know why...".







More information about the Digitalmars-d-learn mailing list