Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. try history

try history

Scheduled Pinned Locked Moved Visual Basic
databasedata-structureshelpquestionworkspace
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    FlorentBzh
    wrote on last edited by
    #1

    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...

    R N 2 Replies Last reply
    0
    • F FlorentBzh

      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...

      R Offline
      R Offline
      Ray Cassick
      wrote on last edited by
      #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.


      My Blog[^]
      FFRF[^]


      F 1 Reply Last reply
      0
      • F FlorentBzh

        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...

        N Offline
        N Offline
        nlarson11
        wrote on last edited by
        #3

        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)

        1 Reply Last reply
        0
        • R Ray Cassick

          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.


          My Blog[^]
          FFRF[^]


          F Offline
          F Offline
          FlorentBzh
          wrote on last edited by
          #4

          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.

          D 1 Reply Last reply
          0
          • F FlorentBzh

            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.

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups