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 10:55:03 PDT 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
}




More information about the Digitalmars-d-learn mailing list