Pathogenix@gmail.com wrote:
[color=blue]
> if exceptions are to be thrown in exceptional circumstances,[/color]
What counts as exceptional is of course up to debate, and different
languages down the years have had different penalties for exceptions, so
I don't think there's a universal consensus on this.
[color=blue]
> is there any pattern for dealing with situations where you need to
> return information on the status of a procedure?[/color]
A reasonable pattern can be seen in Int32.TryParse() and friends.
[color=blue]
> An example might be a login screen where your UI needs to report that
> a) login was successful, b) the password was incorrect, or c) the
> username was not recogised.[/color]
I think it's reasonable in this case to have a method returning boolean
here - in particular, because revealing the exact nature of the
authentication failure is a security weakness. A malicious attacker can
validate usernames without knowing passwords if the response is
different for unknown usernames versus incorrect passwords.
[color=blue]
> Barring the security implications of this, is there any better way than
> returning an enum?[/color]
It entirely depends on how much information you need. If you need lots
of information, it may warrant a rich object graph.
[color=blue]
> What if you don't want to throw an exception because, as in the example
> above, certain classes of error are expected and aren't fatal to an
> app?[/color]
If an error is expected, then you'll want to test for it imperatively -
but you'll still want your mainline case to throw exceptions if the
caller didn't call the corresponding "test" routine. One of the nicer
things about exceptions is that they're harder to ignore than return
values.
[color=blue]
> Would a class to wrap the command make sense in this situation?[/color]
It depends on the circumstances - whatever reduces the total effort of
producing a correct and maintainable implementation that satisfies the
requirements should suffice, I reckon :).
[color=blue]
> I humbly await enlightenment.[/color]
Methinks you protest too much!
-- Barry