Using API methods for simple user input verification increases the risk of application crashes when exceptions are not handled. For example there are several such functions in the IO.Path namespace which may throw an exception if a path string has certain characters in it.
And despite that we know that handling inner exceptions is not a good practice, we found a lot of these constructs in our code:
try { // call some unsafe, foreign, unsure method. } catch(Exception e) { // probably do some logging here. return some error code; }
To relieve paranoia and to centralize logging, a simple generic static method helps here to get around the syntactic distraction above, for example:
public static class Safe { public static ResultT call<ResultT>( ResultT error, Func<ResultT> action) { try { return action(); } catch(Exception e) { // do some logging return error; } } }
Now, if you want to call a method like IsPathRooted in the IO.Path namespace without caring a lot about the exception it throws, you may simply use Safe.call(false, IsPathRooted(pathEntered)).