Re: Design: Custom Exception Usage
- From: "Anthony Jones" <Ant@xxxxxxxxxxxxxxxx>
- Date: Tue, 8 Apr 2008 21:55:55 +0100
"Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx> wrote in message
news:op.t9ataygl8jd0ej@xxxxxxxxxxxxxxxxxxxxxxx
On Tue, 08 Apr 2008 04:28:47 -0700, Anthony Jones <Ant@xxxxxxxxxxxxxxxx>
wrote:
[...]
Secondly, when you want to propogate any messages back to the calling
method, use OUT parameter from C# or use ByRef at VB.NET
That IMO isn't
My advice would be avoid OUT or ByRef parameter like the plague.
Hmmm...
While I'd agree that in this thread, the suggestion to use a by-reference
parameter doesn't make much sense (exceptions are meant to be thrown :) ),
I wouldn't say that the general technique should be avoided "like the
plague". There are too many examples of situations in which a
by-reference parameter is useful and not at all harmful for a broad
generalization like that to be valid.
Hmm... yes perhaps 'plague' is too strong. It is however a bad smell and
one would really have to question a choice to use it.
Taking TryParse as an example I would have prefered to have:-
bool CanParse(string)
but TryParse would clearly be faster. Not having CanParse I'm forced to use
TryParse even in code where performance is not an issue (which frankly is
true of most code).
int x;
string y = "Brass Monkeys";
if (Int32.TryParse(y, out x))
{
// do stuff with x but how did x get assigned a value its not that
obvious
}
Where I would have prefered:-
if (Int32.CanParse(y))
{
x = Int32.Parse(y);
// do stuff with x and its clear where x got its value.
}
OR alternatively
int? x;
string y = "Jon 'Google' Skeets idea'"
x = Int32.ParseOrNull(y)
if(x != null)
{
//do stuff with x
}
Of course its easy enough to build round but it means doing so out of
principle rather than actual need.
--
Anthony Jones - MVP ASP/ASP.NET
.
- Follow-Ups:
- Re: Design: Custom Exception Usage
- From: Peter Duniho
- Re: Design: Custom Exception Usage
- References:
- Design: Custom Exception Usage
- From: Terrence Jones
- Re: Design: Custom Exception Usage
- From: DSK Chakravarthy
- Re: Design: Custom Exception Usage
- From: Anthony Jones
- Re: Design: Custom Exception Usage
- From: Peter Duniho
- Design: Custom Exception Usage
- Prev by Date: Re: Parsing log file using C#
- Next by Date: Re: ListView MouseUp Event
- Previous by thread: Re: Design: Custom Exception Usage
- Next by thread: Re: Design: Custom Exception Usage
- Index(es):
Loading