c# - Passing out failure reason for a commonly failing method -
when throw exception, can put handy message in give more information why operation failed.
obviously throwing exceptions on methods commonly die not practice.
this article on msdn suggests both tryparse
pattern , tester-doer
pattern, neither of these patterns allow extract information why method failed.
is there accepted pattern passing out of safely-failing method allows glean more data of reason failing?
clearly public failurereason tryparsewithmessage(string s, out myclass myclass)
or public bool tryparsewithmessage(string s, out myclass myclass, out failurereason failurereason)
these seem little dirty normal case method succeed...
my suggestion return tuple<statusmessage, myclass>
. you'd do
var result = parsewithstatus(data); if (result.item1 == statusmessage.success) return result.item2; else { // handle each statusmessage case failure. }
or along lines (switch
instead of if/else
, instance).
Comments
Post a Comment