Question about Try - Catch codes.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Patrick Dickey

    #1

    Question about Try - Catch codes.

    Hello, All!
    I'm modifying some source code that I downloaded from
    Planet-source-code a while back, and have a question about Try Catch
    statements. Basically, I'm wondering if I can do a Try with multiple Catch
    statements, or do I have to figure out how to embed Try Catch statements
    into my code. The scenario is this (and I'll paste the actual code at the
    bottom of this post). I'm modifying a web browser, and in the About Help
    box, it has the "System Info" button. In the System Info button, there are
    some GO TO statements that I want to get rid of. Plus, the SysInfoError
    which is referred to by the GO TO statements doesn't tell you anything
    except that an error happened.
    What I'd like to do is put a Try at the beginning of the If-Then
    statements, and for each possible error, put a Catch statement with a
    message saying what the error is. I have a feeling, if I can't do that,
    then a somewhat major rewrite of the subroutine is in order. Here's the
    subroutine, and I appreciate any help that I can get. Also, If I end up
    using snippets of code from here, I will definitely credit the poster.

    Public Sub StartSysInfo()

    On Error GoTo SysInfoErr


    Dim rc As Integer

    Dim SysInfoPath As String


    ' Try To Get System Info Program Path\Name From Registry...

    If GetKeyValue(HKE Y_LOCAL_MACHINE , gREGKEYSYSINFO, gREGVALSYSINFO,
    SysInfoPath) Then

    ' Try To Get System Info Program Path Only From Registry...

    ElseIf GetKeyValue(HKE Y_LOCAL_MACHINE , gREGKEYSYSINFOL OC, gREGVALSYSINFOL OC,
    SysInfoPath) Then

    ' Validate Existance Of Known 32 Bit File Version

    'UPGRADE_WARNIN G: Dir has a new behavior. Click for more:
    'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?ke yword="vbup1041 "'

    If (Dir(SysInfoPat h & "\MSINFO32.EXE" ) <> "") Then

    SysInfoPath = SysInfoPath & "\MSINFO32. EXE"


    ' Error - File Can Not Be Found...

    Else

    GoTo SysInfoErr

    End If

    ' Error - Registry Entry Can Not Be Found...

    Else

    GoTo SysInfoErr

    End If


    Call Shell(SysInfoPa th, AppWinStyle.Nor malFocus)


    Exit Sub

    SysInfoErr:

    MsgBox("System Information Is Unavailable At This Time", MsgBoxStyle.OKO nly)

    End Sub

    Thank you again in advance.
    With best regards, Patrick Dickey. E-mail: pd1ckey43**Remo veThis**@msn.co m




    ---
    avast! Antivirus: Outbound message clean.
    Virus Database (VPS): 0521-3, 05/26/2005
    Tested on: 5/27/2005 1:27:00 AM
    avast! - copyright (c) 1988-2005 ALWIL Software.
    Join hundreds of millions of others & get free antivirus for PC, Mac, & Android. Surf safely with our VPN. Download Avast!




  • Oenone

    #2
    Re: Question about Try - Catch codes.

    Patrick Dickey wrote:[color=blue]
    > Basically, I'm wondering if I can do a Try with multiple
    > Catch statements[/color]

    Yes you can. For example:

    \\\
    Try
    [your code here]

    Catch ex As OverflowExcepti on
    [handle this type of error]

    Catch ex As NullReferenceEx ception
    [handle this type of error]

    Catch ex As Exception
    [handle all other error types]

    End Try
    ///

    --

    (O) e n o n e


    Comment

    • Terry Burns

      #3
      Re: Question about Try - Catch codes.

      These two methods of error handling are completely different. The Try Catch
      model relies upon exception handling, in other words when an exception is
      thrown by a class or method, this can be seen inside the Try block and
      caught by one or more Catch blocks. See example below.

      Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button1.Click
      Try

      throwit1()

      throwit2()

      Catch ex As Exp1

      MsgBox("1")

      Catch ex As Exp2

      MsgBox("2")

      End Try

      End Sub

      Sub throwit1()

      Throw New Exp1

      End Sub

      Sub throwit2()

      Throw New Exp2

      End Sub

      If however you insist on using the OnError model, then you could OnErro Goto
      YourErrorHandli ngCode: and use the Select Case Statement

      Select Case YourErrorNumber
      Case nn
      Case nn
      Case Else

      End Case

      This is a simplified answer, but it should put u in the right direction

      HTH
      --
      Terry Burns

      "Patrick Dickey" <pd1ckey43**Rem oveThis**@msn.c om> wrote in message
      news:Os7WXUoYFH A.2588@TK2MSFTN GP14.phx.gbl...[color=blue]
      > Hello, All!
      > I'm modifying some source code that I downloaded from
      > Planet-source-code a while back, and have a question about Try Catch
      > statements. Basically, I'm wondering if I can do a Try with multiple
      > Catch statements, or do I have to figure out how to embed Try Catch
      > statements into my code. The scenario is this (and I'll paste the actual
      > code at the bottom of this post). I'm modifying a web browser, and in the
      > About Help box, it has the "System Info" button. In the System Info
      > button, there are some GO TO statements that I want to get rid of. Plus,
      > the SysInfoError which is referred to by the GO TO statements doesn't tell
      > you anything except that an error happened.
      > What I'd like to do is put a Try at the beginning of the If-Then
      > statements, and for each possible error, put a Catch statement with a
      > message saying what the error is. I have a feeling, if I can't do that,
      > then a somewhat major rewrite of the subroutine is in order. Here's the
      > subroutine, and I appreciate any help that I can get. Also, If I end up
      > using snippets of code from here, I will definitely credit the poster.
      >
      > Public Sub StartSysInfo()
      >
      > On Error GoTo SysInfoErr
      >
      >
      > Dim rc As Integer
      >
      > Dim SysInfoPath As String
      >
      >
      > ' Try To Get System Info Program Path\Name From Registry...
      >
      > If GetKeyValue(HKE Y_LOCAL_MACHINE , gREGKEYSYSINFO, gREGVALSYSINFO,
      > SysInfoPath) Then
      >
      > ' Try To Get System Info Program Path Only From Registry...
      >
      > ElseIf GetKeyValue(HKE Y_LOCAL_MACHINE , gREGKEYSYSINFOL OC,
      > gREGVALSYSINFOL OC, SysInfoPath) Then
      >
      > ' Validate Existance Of Known 32 Bit File Version
      >
      > 'UPGRADE_WARNIN G: Dir has a new behavior. Click for more:
      > 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?ke yword="vbup1041 "'
      >
      > If (Dir(SysInfoPat h & "\MSINFO32.EXE" ) <> "") Then
      >
      > SysInfoPath = SysInfoPath & "\MSINFO32. EXE"
      >
      >
      > ' Error - File Can Not Be Found...
      >
      > Else
      >
      > GoTo SysInfoErr
      >
      > End If
      >
      > ' Error - Registry Entry Can Not Be Found...
      >
      > Else
      >
      > GoTo SysInfoErr
      >
      > End If
      >
      >
      > Call Shell(SysInfoPa th, AppWinStyle.Nor malFocus)
      >
      >
      > Exit Sub
      >
      > SysInfoErr:
      >
      > MsgBox("System Information Is Unavailable At This Time",
      > MsgBoxStyle.OKO nly)
      >
      > End Sub
      >
      > Thank you again in advance.
      > With best regards, Patrick Dickey. E-mail:
      > pd1ckey43**Remo veThis**@msn.co m
      >
      >
      >
      > ---
      > avast! Antivirus: Outbound message clean.
      > Virus Database (VPS): 0521-3, 05/26/2005
      > Tested on: 5/27/2005 1:27:00 AM
      > avast! - copyright (c) 1988-2005 ALWIL Software.
      > http://www.avast.com
      >
      >
      >[/color]


      Comment

      • Patrick Dickey

        #4
        Re: Question about Try - Catch codes.

        Hello, Patrick!
        You wrote to All on Fri, 27 May 2005 01:26:58 -0500:

        [Sorry, skipped to conserve space]

        PD> Public Sub StartSysInfo()
        PD>
        PD> On Error GoTo SysInfoErr
        PD>

        PD> Dim rc As Integer
        PD>
        PD> Dim SysInfoPath As String
        PD> ' Try To Get System Info Program Path\Name From Registry...
        PD>
        PD> If GetKeyValue(HKE Y_LOCAL_MACHINE , gREGKEYSYSINFO, gREGVALSYSINFO,
        PD>
        PD> SysInfoPath) Then
        PD> ' Try To Get System Info Program Path Only From
        PD> Registry...
        PD> ElseIf GetKeyValue(HKE Y_LOCAL_MACHINE ,
        PD> gREGKEYSYSINFOL OC,
        PD> gREGVALSYSINFOL OC,
        PD> SysInfoPath) Then
        PD> '
        PD> Validate Existance Of Known 32 Bit File Version
        PD> 'UPGRADE_WARNIN G: Dir
        PD> has a new behavior. Click for more:
        PD>
        PD> 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?ke yword="
        PD>
        PD> vbup1041"'
        PD> If (Dir(SysInfoPat h & "\MSINFO32.EXE" ) <> "") Then
        PD>
        PD> SysInfoPath = SysInfoPath & "\MSINFO32. EXE"
        PD> ' Error - File Can Not Be
        PD> Found...
        PD> Else
        PD> GoTo SysInfoErr
        PD> End If
        PD> ' Error - Registry
        PD> Entry Can Not Be Found...
        PD> Else
        PD> GoTo SysInfoErr
        PD> End If

        PD> Call
        PD> Shell(SysInfoPa th, AppWinStyle.Nor malFocus)
        PD> Exit Sub
        PD>
        PD> SysInfoErr:
        PD> MsgBox("System Information Is Unavailable At This
        PD> Time",
        PD> MsgBoxStyle.OKO nly)
        PD> End Sub

        [Sorry, skipped to conserve space]

        Ok, I'm going to revamp this a little bit. For Terry's reply, I need to
        clarify that I'm trying to eliminate all GOTO's-- Including the OnError Goto
        statement. Bascially, I've ran a project analyzer on the code, and the
        GOTO's are one thing it flagged. Plus, waaaaaaaaaaaaay back when I learned
        programming (COBOL, Pascal, ApplesoftBASIC, FORTRAN, etc.) I was taught that
        there is almost always a better way of coding then using GOTO. GOTO is
        evil.. And, in most cases, the instructor would deduct points for GOTO
        statements. Spaghetti programming, is the term we used for GOTO's.

        So, here's what I'm thinking about doing. I'll recopy only the appropriate
        IF-Then statements, and the end of the subroutine, and get everyone's
        opinions. Thank you again, in advance.
        Dim ErrorLevel as Integer
        ErrorLevel = 0
        If GetKeyValue(HKE Y_LOCAL_MACHINE , gREGKEYSYSINFO, gREGVALSYSINFO,
        SysInfoPath) Then
        ' Try To Get System Info Program Path Only From Registry...
        ElseIf GetKeyValue(HKE Y_LOCAL_MACHINE , gREGKEYSYSINFOL OC,
        gREGVALSYSINFOL OC, SysInfoPath) Then
        ' Validate Existance Of Known 32 Bit File Version
        If (Dir(SysInfoPat h & "\MSINFO32.EXE" ) <> "") Then
        SysInfoPath = SysInfoPath & "\MSINFO32. EXE"
        Else
        ' Error - File Can Not Be Found...
        ErrorLevel = 2
        End If
        Else
        ErrorLevel = 1
        ' Error - Registry Entry Can Not Be Found...
        End If

        IF ErrorLevel = 1 Then
        MsgBox("The Registry entry could not be found.", MsgBoxStyle.OKO nly)
        ElseIF ErrorLevel = 2 Then
        MsgBox("The Program File could not be found.", MsgBoxStyle.OKO nly)
        Endif
        EndIf

        Basically, the only changes I want to make are creating an integer called
        ErrorLevel, initializing it to 0 at the beginning, and then setting a value
        for it, instead of using the "GOTO SysError" statements, in the IF-Then
        statements.

        With best regards, Patrick Dickey. E-mail: pd1ckey43**Remo veThis**@msn.co m




        ---
        avast! Antivirus: Outbound message clean.
        Virus Database (VPS): 0522-1, 05/31/2005
        Tested on: 5/31/2005 5:37:11 AM
        avast! - copyright (c) 1988-2005 ALWIL Software.
        Join hundreds of millions of others & get free antivirus for PC, Mac, & Android. Surf safely with our VPN. Download Avast!




        Comment

        • Terry Burns

          #5
          Question about Try - Catch codes.

          I would be more inclined to create an exception expecially for this and
          throw it in your GetKeyValue calling function. Think about an approach which
          looks something along these lines.

          Try

          GetKeyValue(HKE Y_LOCAL_MACHINE , gREGKEYSYSINFO,
          gREGVALSYSINFO, SysInfoPath) Then
          GetKeyValue(HKE Y_LOCAL_MACHINE , gREGKEYSYSINFOL OC,gREGVALSYSIN FOLOC,
          SysInfoPath) Then

          If (Dir(SysInfoPat h & "\MSINFO32.EXE" ) <> "") Then
          SysInfoPath = SysInfoPath & "\MSINFO32. EXE"
          Else
          Throw new FileNotFoundExc eption . . . . . . . . . stuff . . . . . .
          End If

          Catch ex as GetKeyValueExce ption

          MsgBox ex.message ' or Some error handling code.

          Catch ex as FileNotFoundExc eption

          End Try

          --
          Terry Burns

          "Patrick Dickey" <pd1ckey43**Rem oveThis**@msn.c om> wrote in message
          news:ebDHxycZFH A.3908@TK2MSFTN GP15.phx.gbl...[color=blue]
          > Hello, Patrick!
          > You wrote to All on Fri, 27 May 2005 01:26:58 -0500:
          >
          > [Sorry, skipped to conserve space]
          >
          > PD> Public Sub StartSysInfo()
          > PD>
          > PD> On Error GoTo SysInfoErr
          > PD>
          >
          > PD> Dim rc As Integer
          > PD>
          > PD> Dim SysInfoPath As String
          > PD> ' Try To Get System Info Program Path\Name From Registry...
          > PD>
          > PD> If GetKeyValue(HKE Y_LOCAL_MACHINE , gREGKEYSYSINFO, gREGVALSYSINFO,
          > PD>
          > PD> SysInfoPath) Then
          > PD> ' Try To Get System Info Program Path Only From
          > PD> Registry...
          > PD> ElseIf GetKeyValue(HKE Y_LOCAL_MACHINE ,
          > PD> gREGKEYSYSINFOL OC,
          > PD> gREGVALSYSINFOL OC,
          > PD> SysInfoPath) Then
          > PD> '
          > PD> Validate Existance Of Known 32 Bit File Version
          > PD> 'UPGRADE_WARNIN G: Dir
          > PD> has a new behavior. Click for more:
          > PD>
          > PD> 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?ke yword="
          > PD>
          > PD> vbup1041"'
          > PD> If (Dir(SysInfoPat h & "\MSINFO32.EXE" ) <> "") Then
          > PD>
          > PD> SysInfoPath = SysInfoPath & "\MSINFO32. EXE"
          > PD> ' Error - File Can Not Be
          > PD> Found...
          > PD> Else
          > PD> GoTo SysInfoErr
          > PD> End If
          > PD> ' Error - Registry
          > PD> Entry Can Not Be Found...
          > PD> Else
          > PD> GoTo SysInfoErr
          > PD> End If
          >
          > PD> Call
          > PD> Shell(SysInfoPa th, AppWinStyle.Nor malFocus)
          > PD> Exit Sub
          > PD>
          > PD> SysInfoErr:
          > PD> MsgBox("System Information Is Unavailable At This
          > PD> Time",
          > PD> MsgBoxStyle.OKO nly)
          > PD> End Sub
          >
          > [Sorry, skipped to conserve space]
          >
          > Ok, I'm going to revamp this a little bit. For Terry's reply, I need to
          > clarify that I'm trying to eliminate all GOTO's-- Including the OnError
          > Goto statement. Bascially, I've ran a project analyzer on the code, and
          > the GOTO's are one thing it flagged. Plus, waaaaaaaaaaaaay back when I
          > learned programming (COBOL, Pascal, ApplesoftBASIC, FORTRAN, etc.) I was
          > taught that there is almost always a better way of coding then using GOTO.
          > GOTO is evil.. And, in most cases, the instructor would deduct points for
          > GOTO statements. Spaghetti programming, is the term we used for GOTO's.
          >
          > So, here's what I'm thinking about doing. I'll recopy only the
          > appropriate IF-Then statements, and the end of the subroutine, and get
          > everyone's opinions. Thank you again, in advance.
          > Dim ErrorLevel as Integer
          > ErrorLevel = 0
          > If GetKeyValue(HKE Y_LOCAL_MACHINE , gREGKEYSYSINFO, gREGVALSYSINFO,
          > SysInfoPath) Then
          > ' Try To Get System Info Program Path Only From Registry...
          > ElseIf GetKeyValue(HKE Y_LOCAL_MACHINE , gREGKEYSYSINFOL OC,
          > gREGVALSYSINFOL OC, SysInfoPath) Then
          > ' Validate Existance Of Known 32 Bit File Version
          > If (Dir(SysInfoPat h & "\MSINFO32.EXE" ) <> "") Then
          > SysInfoPath = SysInfoPath & "\MSINFO32. EXE"
          > Else
          > ' Error - File Can Not Be Found...
          > ErrorLevel = 2
          > End If
          > Else
          > ErrorLevel = 1
          > ' Error - Registry Entry Can Not Be Found...
          > End If
          >
          > IF ErrorLevel = 1 Then
          > MsgBox("The Registry entry could not be found.", MsgBoxStyle.OKO nly)
          > ElseIF ErrorLevel = 2 Then
          > MsgBox("The Program File could not be found.", MsgBoxStyle.OKO nly)
          > Endif
          > EndIf
          >
          > Basically, the only changes I want to make are creating an integer called
          > ErrorLevel, initializing it to 0 at the beginning, and then setting a
          > value for it, instead of using the "GOTO SysError" statements, in the
          > IF-Then statements.
          >
          > With best regards, Patrick Dickey. E-mail:
          > pd1ckey43**Remo veThis**@msn.co m
          >
          >
          >
          > ---
          > avast! Antivirus: Outbound message clean.
          > Virus Database (VPS): 0522-1, 05/31/2005
          > Tested on: 5/31/2005 5:37:11 AM
          > avast! - copyright (c) 1988-2005 ALWIL Software.
          > http://www.avast.com
          >
          >
          >[/color]


          Comment

          • Phill.  W

            #6
            Re: Question about Try - Catch codes.

            "Patrick Dickey" <pd1ckey43**Rem oveThis**@msn.c om> wrote in message
            news:Os7WXUoYFH A.2588@TK2MSFTN GP14.phx.gbl...
            .. . .[color=blue]
            > Basically, I'm wondering if I can do a Try with multiple Catch
            > statements,[/color]

            Yes you can, with one proviso - you have to "know" which
            Exceptions derive from which other Exceptions and make sure
            that you Catch the "smallest" one first, as in

            Try
            ' Do Something database-y
            Catch ox as System.Data.Odb c.OdbcException
            Catch sx as System.SystemEx ception
            Catch ex as System.Exceptio n
            Finally
            End Try

            Each of these Exception classes is inherited from the next and, when
            an Exception is thrown, the program will use the "most specific"
            Exception Handler. If you were to put

            Catch ex as System.Exceptio n

            first, that's the /only/ handler that would ever get run.

            HTH,
            Phill W.


            Comment

            Working...