Sometimes, exception is not a bad thing. A code within library could return Option<T, TException>, but the exception cannot be properly handled in calling code, so just fall-back to C# exception handling by throwing this exception like normally using unsafe method.
One problem is that TException can be of any type and not just Exception. My suggestion is : If TException is Exception, then throw InvalidOperationException (or something similar) and exception as inner exception. If it isn't Exception, then create new Exception type and save the exception as object within it.
Actually. I just realized this is stupid suggestion. It would work if the Option<T, TException> was tri-state : value, null or exception. Which it is not.
Sometimes, exception is not a bad thing. A code within library could return
Option<T, TException>
, but the exception cannot be properly handled in calling code, so just fall-back to C# exception handling by throwing this exception like normally using unsafe method.One problem is that
TException
can be of any type and not justException
. My suggestion is : IfTException
isException
, then throwInvalidOperationException
(or something similar) and exception as inner exception. If it isn'tException
, then create new Exception type and save the exception as object within it.