about Exceptions

krispaks

Member
Joined
Jan 22, 2007
Messages
17
Programming Experience
1-3
I have this function which will accept a time as string

Public Shared Function SpecTime(ByVal timeStr As String) As String
Dim dt As DateTime
If String.IsNullOrEmpty(timeStr) Then
Return ""
Else
Try
dt = DateTime.Parse("1/8/1985 " + timeStr)
Catch ex As Exception
'catches a FormatException
End Try
Return dt.ToString("h:mm tt")
End If
End Function

when i input an invalid time str eg. "1a", how do i know the unique code for that specific exception? how will it know that "12 a" is invalid or "12;00" is invalid???

my point here is that i need to know the unique exception code (if there is any) so i could handle the different exceptions accordingly.

tnx!
 
Why use a string to pass a date or time?

I would use a datetime variable and pass the time portion that way you have no conversion errors or problems to deal with.
 
VB.NET:
Try

Catch fex As FormatException

Catch ex As Exception

End Try
 
Why use a string to pass a date or time?

I would use a datetime variable and pass the time portion that way you have no conversion errors or problems to deal with.


the actual setup of the application is that, i would be using a textbox. and you could input anything on it. and it will automatically correct itself, if it can.

ex. if i input the ff strings on the textbox it would be changed to its corresponding correct form:

inputted string: | correct string:
1a | 1:00 AM
1 p | 1:00 PM
10;00 | 10:00 AM

when i tried to parse the inputted strings as datetime, it would throw an exception (FormatException). but the messages are different. i want to how it was able to distinguish that the error was because of the semicolon or that it would not accept "1a" or "1 p"
 
Back
Top