
Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
>>> Why raise an exception when TryParseExact does exactly what you need to
>>> do, but without throwing?
>> I would say it depends on what you're doing.
>
> Well, look at the context of the previous post - Pipo's code was
> swallowing the exception and returning false.
I assumed that it was just a snippet and in real code some error
handling would go in the catch block. :-)
> TryParseExact has the
> same behaviour but with less hassle and better performance.
[quoted text clipped - 6 lines]
> input is a valid date or not. When you're expecting potentially invalid
> input, an exception isn't appropriate IMO.
I suspect I disagree with you there...
If I have some networking protocol and the spec says that I should get a
string that looks like a date, throwing an exception seems entirely
reasonable.
If I receive user input that I expect to be a date, I think throwing an
exception is probably reasonable - it clearly states that something went
wrong, we can't proceed, and all we can do is error handling (probably
informing the user in this case).
If I have a random bunch of strings and I want to find out which are
valid dates, it's time to use TryParseExact.
Alun Harford
Jon Skeet [C# MVP] - 12 Mar 2008 01:16 GMT
> > Well, look at the context of the previous post - Pipo's code was
> > swallowing the exception and returning false.
>
> I assumed that it was just a snippet and in real code some error
> handling would go in the catch block. :-)
Nope, just answering the OP's question. Both mine and Pipo's code do
that, but mine is cleaner IMO.
> > Look at the OP - the whole point of the thread is to *test* whether the
> > input is a valid date or not. When you're expecting potentially invalid
> > input, an exception isn't appropriate IMO.
>
> I suspect I disagree with you there...
Possibly.
> If I have some networking protocol and the spec says that I should get a
> string that looks like a date, throwing an exception seems entirely
> reasonable.
In that case I'd agree.
> If I receive user input that I expect to be a date, I think throwing an
> exception is probably reasonable - it clearly states that something went
> wrong, we can't proceed, and all we can do is error handling (probably
> informing the user in this case).
I don't think an exception is called for in that case. Chances are:
a) you're going to be explicitly testing for that case
b) you're not going to use anything about the exception itself,
other than the fact it was thrown. The details are irrelevant.
I think in that case using TryParseExact would be the right way to go.
> If I have a random bunch of strings and I want to find out which are
> valid dates, it's time to use TryParseExact.
I don't think that happens often, but validating user input does - and
I don't think it merits an exception.
I don't *expect* users to get things right :)

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk