try history
-
Hello everybody, I try to use this "try catch" block :
Function DateConvert(ByVal strDate As String) As String Dim jj, mm, aa As String Dim buf() As String Try buf = Split(strDate, "/") jj = buf(0) mm = buf(1) aa = buf(2) Catch ex As Exception Throw ex End Try If (System.Web.Configuration.WebConfigurationManager.AppSettings("OS") = "US") Then DateConvert = mm & "/" & jj & "/" & aa Else DateConvert = jj & "/" & mm & "/" & aa End If End Function
When I pass a string with a bad date format like "02/2006" or "" instead of "23/10/2006" I get this error : "Index was outside the bounds of the array." The "try catch" block must catch this type of error but not there... why ? Please, my code doesn't look bad but I don't find where I'm wrong... PS1 : I also try with an IndexOutOfRangeException exception with the same result !! PS2 : I use the Framework 2.0... -
Hello everybody, I try to use this "try catch" block :
Function DateConvert(ByVal strDate As String) As String Dim jj, mm, aa As String Dim buf() As String Try buf = Split(strDate, "/") jj = buf(0) mm = buf(1) aa = buf(2) Catch ex As Exception Throw ex End Try If (System.Web.Configuration.WebConfigurationManager.AppSettings("OS") = "US") Then DateConvert = mm & "/" & jj & "/" & aa Else DateConvert = jj & "/" & mm & "/" & aa End If End Function
When I pass a string with a bad date format like "02/2006" or "" instead of "23/10/2006" I get this error : "Index was outside the bounds of the array." The "try catch" block must catch this type of error but not there... why ? Please, my code doesn't look bad but I don't find where I'm wrong... PS1 : I also try with an IndexOutOfRangeException exception with the same result !! PS2 : I use the Framework 2.0...First off, it looks to me like you are just catch the exception and then rethrowing it so I am not sure what you are expecting to happen there. Second, IMHO it would be better to validate the string a bit and even check to see if after the split there are enough array parts before trying to access them instead of just throwing an exception there.
-
Hello everybody, I try to use this "try catch" block :
Function DateConvert(ByVal strDate As String) As String Dim jj, mm, aa As String Dim buf() As String Try buf = Split(strDate, "/") jj = buf(0) mm = buf(1) aa = buf(2) Catch ex As Exception Throw ex End Try If (System.Web.Configuration.WebConfigurationManager.AppSettings("OS") = "US") Then DateConvert = mm & "/" & jj & "/" & aa Else DateConvert = jj & "/" & mm & "/" & aa End If End Function
When I pass a string with a bad date format like "02/2006" or "" instead of "23/10/2006" I get this error : "Index was outside the bounds of the array." The "try catch" block must catch this type of error but not there... why ? Please, my code doesn't look bad but I don't find where I'm wrong... PS1 : I also try with an IndexOutOfRangeException exception with the same result !! PS2 : I use the Framework 2.0...the reason why you are getting "Index was outside the bounds of the array." is because you have hardcoded your code to assume there will always be 3 pieces of the date present. jj = buf(0) mm = buf(1) 'the below will not be available when you pass in 02/2006...there is no second slash present to make the array go up to an upperbound of 2. aa = buf(2)
-
First off, it looks to me like you are just catch the exception and then rethrowing it so I am not sure what you are expecting to happen there. Second, IMHO it would be better to validate the string a bit and even check to see if after the split there are enough array parts before trying to access them instead of just throwing an exception there.
Thanks for your answer, I'm OK that the way I try to use is not really "beautiful", but what I want to know is why, when an exception occures, this Exception is not treated by the catch block ? I just want to throw it to the call function and treat this exception in the call function.
-
Thanks for your answer, I'm OK that the way I try to use is not really "beautiful", but what I want to know is why, when an exception occures, this Exception is not treated by the catch block ? I just want to throw it to the call function and treat this exception in the call function.
It being treated by the catch block. The only thing you're doing is re-throwing the same exception, which isn't being handled by the code that called the code snippet you posted.
Dave Kreskowiak Microsoft MVP - Visual Basic