This is an idea of handling methods that can return error without using pattern TryMethod(out string error).
In this concept, we have 4 types of methods that does:
- not return anything and cannot fail (void methods)
- return some value and cannot fail (non-void methods)
- not return anything and can fail (returning Result)
- return some value and can fail (returning Result<T>)
pubilicstatic void Main(string[]args){ResultfirstMethodResult=VoidMethodThatCanReturnError();if(firstMethodResult.IsOk){Console.WriteLine("first method finished without an error");}else{Console.WriteLine(firstMethodResult.ErrorMsg);}Result<Person>secondMethodResult=ReturningMethodThatCanReturnError();if(secondMethodResult.IsError){Console.WriteLine(secondMethodResult.ErrorMsg);}else{Console.WriteLine(secondMethodResult.Value.Name);}}privateResultVoidMethodThatCanReturnError(){if(condition){returnResult.Error("error message")}else{returnResult.Ok();}}privateResult<Person>ReturningMethodThatCanReturnError(){try{varperson=newPerson("John","Doe");returnResult.Ok(person);}catch(Exceptionex){returnResult.Error(ex.Message);}}