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

k-five via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat May 13 01:50:20 PDT 2017


On Saturday, 13 May 2017 at 02:40:17 UTC, Mike B Johnson wrote:
>
> 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...".

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

Way arguing when a simple code can clarify the subject? right?
If am not clear so consider me as an stupid man, no problem at 
all.

but CAN you please solve it for me?

import std.stdio:	writeln;
import std.conv:	to;

void main( string[] args ){
	
	string str = "10";
	int index;
	index = to!int( str );	// okay, no exception are thrown
	
	str = "some-words";
	index = to!int( str ); // problem, an exception is thrown
	
}

Please run this code and convert "some-words" to int by using to! 
function without facing any exception. Thanks



More information about the Digitalmars-d-learn mailing list