IsDate() error

O

Oenone

I'm having a couple of problems with the IsDate function in VB.net (.net
framework v1.1). Having searched the web and usenet for others having the
same problem, I've seen several posts from people reporting the same error
as me, followed by various responses from others saying they can't recreate
the problem.

Well I think I know how to recreate it, but I still don't understand why
it's occurring. Does anyone have any ideas?

Try the following code:

Dim t As Object = DBNull.Value
If IsDate(t.ToString) Then
MsgBox("Computer says yes")
Else
MsgBox("Computer says no")
End If

If you run this with Common Language Runtime Exceptions set to Continue, it
works just fine and the computer says no. However, if you switch it to Break
on CLR Exceptions, when the IsDate() is executed the following exception is
thrown:

A first chance exception of type 'System.FormatException' occurred in
mscorlib.dll
Additional information: String was not recognized as a valid DateTime.

Clicking Continue produces a second exception:

A first chance exception of type 'System.InvalidCastException' occurred
in microsoft.visualbasic.dll
Additional information: Cast from string "" to type 'Date' is not valid.

....clicking Continue again results in the computer saying no. If you wrap a
Try/Catch statement around the code, the Catch block does not get called.

This is very confusing to me, I've not seen VB.net act in this way before.
It seems that the IsDate() function raises an exception but then somehow
still handles it and carries on regardless. Even when CLR Exceptions are set
to Continue, there's still the tell-tale delay that occurs when an exception
is thrown when running in the IDE, even though the program carries on
without error.

Can anyone shed any light on this for me?
 
D

David Browne

Oenone said:
I'm having a couple of problems with the IsDate function in VB.net (.net
framework v1.1). Having searched the web and usenet for others having the
same problem, I've seen several posts from people reporting the same error
as me, followed by various responses from others saying they can't
recreate the problem.

Well I think I know how to recreate it, but I still don't understand why
it's occurring. Does anyone have any ideas?

Try the following code:

Dim t As Object = DBNull.Value
If IsDate(t.ToString) Then
MsgBox("Computer says yes")
Else
MsgBox("Computer says no")
End If

If you run this with Common Language Runtime Exceptions set to Continue,
it works just fine and the computer says no. However, if you switch it to
Break on CLR Exceptions, when the IsDate() is executed the following
exception is thrown:

A first chance exception of type 'System.FormatException' occurred in
mscorlib.dll
Additional information: String was not recognized as a valid DateTime.

Clicking Continue produces a second exception:

A first chance exception of type 'System.InvalidCastException' occurred
in microsoft.visualbasic.dll
Additional information: Cast from string "" to type 'Date' is not
valid.

...clicking Continue again results in the computer saying no. If you wrap
a Try/Catch statement around the code, the Catch block does not get
called.

This is very confusing to me, I've not seen VB.net act in this way before.
It seems that the IsDate() function raises an exception but then somehow
still handles it and carries on regardless. Even when CLR Exceptions are
set to Continue, there's still the tell-tale delay that occurs when an
exception is thrown when running in the IDE, even though the program
carries on without error.

Can anyone shed any light on this for me?

You've pretty much got it. IsDate is implemented something like this:

function IsDate(s as string) as boolean
try
dim d as DateTime = DateTime.Parse(s)
return true
catch ex as Exception
return false
end try
end function

IsDate generates an exception on an invalid date, but then catches it and
returns false. If you have first chance exceptions turned on in the
debugger you get a break on the exceptions, but they are thrown and handled
in any case.

David
 
O

Oenone

David Browne said:
IsDate generates an exception on an invalid date, but then catches it and
returns false. If you have first chance exceptions turned on in the
debugger you get a break on the exceptions, but they are thrown and
handled in any case.

That's interesting -- and slightly annoying. :)

Is IsDate() unusual in that it can generate exceptions that are caught by
the IDE? I've not seen any other functions that do this.

Are there any other ways to check whether a string can be parsed as a date
that don't generate exceptions? I usually like to run my code (especially
when first writing it) with the IDE set to break on exceptions so that I can
more easily diagnose any problems I encounter. Having it break every time it
checks a date string is infuriating...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top