Uncle Bob gives the example below in his book Clean Code. Whenever we expect an exception to be thrown we can use the Try
prefix to a method name:
public void sendShutDown()
{
try{
tryToShutDown();
} catch (DeviceShutDownError e) {
logger.log(e);
}
}
And then (adapted):
private void tryToShutDown()
{
//some code with no error handling, but
//something might go wrong here
}
The tryToShutDown
method does not make any error handling, because that's the responsibility of the sendShutDown
method.
The TryParse
pattern of Microsoft violates the clean code guideline that says that we should avoid output parameters.
If we're not developing a new version of C#, we don't have to stick to all Microsoft guidelines. Sometimes they're not the best.