I am currently trying to work out a 'TryMapOrReturnDefault' pattern, I already have a map method which throws an exception in case it fails, but I need in some cases to have it return something (either user specified or null by default)
but how I do actually do it,
It seem wrong to "just" wrap the map method in a try finally, and in case an exception is being triggered return default value or am I wrong here - seem like I am misusing the fact an exception is being triggered, or is that the way to do it?
public static CustomModel? TryMapOrDefault(FieldType value, string title, CustomModel? defaultValue = null)
{
CustomModel? returnValue = defaultValue;
try
{
returnValue = Map(value, title);
}
finally
{
}
return returnValue;
}