1

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;
}
I am not Fat
  • 283
  • 11
  • 36

1 Answers1

1

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

In my view, you can use try-catch statement because you have plan B when your program meet an exception. So, it is perfectly eligible to me to use try-catch statement here because you can return default value and this is plan b.

It is not the case when an error is unrecoverable. E.g., you cannot read data from table because table was dropped recently from database. On the other hand, if error can be recovered, then you can go with try-catch

UPDATE:

If Map function does not throw exception, then it is possible to check null and then just return default value:

public static CustomModel? TryMapOrDefault(FieldType value, string title, 
    CustomModel? defaultValue = null)
{
    CustomModel? returnValue = defaultValue;
    
    returnValue = Map(value, title);

    if (returnValue is null) 
        returnValue = defaultValue; 

    return returnValue;
}

If you want to return defaultValue when Exception is thrown, then you can use catch statement to assign default value.

Let me show an example:

public static CustomModel? TryMapOrDefault(FieldType value, string title, 
    CustomModel? defaultValue = null)
{
    CustomModel? returnValue = defaultValue;
    try
    {
        returnValue = Map(value, title);
    }
    catch (Exception ) {
        returnValue = defaultValue; 
    }
    return returnValue;
}
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • The question lies in whether it is correct to rely on triggering an exception to find out whether you able to map or not - is it correct to trigger an exception on purpose to return a default value - or should one have evaluated the input parameter with if statements to then return default value? – I am not Fat Oct 19 '22 at 11:20
  • @IamnotFat does your `Map` function throws exception? Please, see my updated answer – StepUp Oct 19 '22 at 11:27
  • Yes, it throws an exception in case it fails. It evaluated whether the input params are null and throws nullexception – I am not Fat Oct 19 '22 at 11:27
  • @IamnotFat *should one have evaluated the input parameter with if statements to then return default value*, I think, you can create a Dictionary for that case, but I would prefer `try catch` statement – StepUp Oct 19 '22 at 11:32
  • Using exceptions for control flow is considered as an anti-pattern. https://softwareengineering.stackexchange.com/questions/189222/are-exceptions-as-control-flow-considered-a-serious-antipattern-if-so-why – I am not Fat Oct 24 '22 at 12:52
  • @IamnotFat I think, it is very debatable theme. It depends on many factors, e.g. when it is used, what it is going after exception handling. I am trying to stick with this point [Why not use exceptions as regular flow of control?](https://stackoverflow.com/a/729411) – StepUp Oct 24 '22 at 13:26