I'm trying to avoid returning an incorrect value when in the catch but I'm having trouble finding a better solution than this:
private SecurityLevel ApiGetSecurityLevel()
{
try
{
return _BioidInstance.GetSecurityLevel();
}
catch
{
return SecurityLevel.High;
}
}
Is there a better way of doing this so I don't return incorrect values? I can't change the SecurityLevel enum.
Do not catch the exception. Allow the exception to "bubble-up" to force the caller/callers to handle setting the default security value.
If you really want to return a value then use Nullable<SecurityLevel>
or SecurityLevel?
.
private SecurityLevel? ApiGetSecurityLevel() {
try {
return _BioidInstance.GetSecurityLevel();
}
catch {
return null;
}
}
Then use as:
if (ApiGetSecurityLevel().HasValue == false) {
// use default security level
}
Is it possible this is a case where the application should just fail? That is, if the SecurityLevel can't be determined, the user shouldn't be able to continue?
If so, why no just re-throw and let the UI handle it (or let it get logged, however your shop works)?
If the application should be able to continue, pick (and document) some default value and do exactly what you're doing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With