Will code after a Throw ever run?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff Johnson [MVP: VB]

    Will code after a Throw ever run?

    My testing seems to indicate that it won't, but no documentation I can find
    explicitly states this. Let's say I throw a customer exception like this:

    Try
    m = New MyObject
    Catch ex As Exception
    Throw New MyObjectCreatio nFailedExceptio n(<stuff>)

    LaunchNuclearMi ssiles()
    End Try

    Will the world ever get annihilated?

    (I don't know WHY I thought I could put cleanup code after a Throw
    statement, but I tried anyway and while stepping through in the debugger I
    noticed that I was jumping out of the procedure as soon as the error was
    thrown. Oops!)


  • Charles Law

    #2
    Re: Will code after a Throw ever run?

    Hi Jeff

    Just one statement missing:

    Try
    m = New MyObject
    Catch ex As Exception
    Throw New MyObjectCreatio nFailedExceptio n(<stuff>)
    Finally
    LaunchNuclearMi ssiles()
    End Try

    HTH

    Charles


    "Jeff Johnson [MVP: VB]" <i.get@enough.s pam> wrote in message
    news:Owvk3CXeDH A.2364@TK2MSFTN GP09.phx.gbl...[color=blue]
    > My testing seems to indicate that it won't, but no documentation I can[/color]
    find[color=blue]
    > explicitly states this. Let's say I throw a customer exception like this:
    >
    > Try
    > m = New MyObject
    > Catch ex As Exception
    > Throw New MyObjectCreatio nFailedExceptio n(<stuff>)
    >
    > LaunchNuclearMi ssiles()
    > End Try
    >
    > Will the world ever get annihilated?
    >
    > (I don't know WHY I thought I could put cleanup code after a Throw
    > statement, but I tried anyway and while stepping through in the debugger I
    > noticed that I was jumping out of the procedure as soon as the error was
    > thrown. Oops!)
    >
    >[/color]


    Comment

    • Jeff Johnson [MVP: VB]

      #3
      Re: Will code after a Throw ever run?


      "Charles Law" <blah@thingummy .com> wrote in message
      news:Oj1DMGXeDH A.2304@TK2MSFTN GP10.phx.gbl...
      [color=blue]
      > Just one statement missing:
      >
      > Try
      > m = New MyObject
      > Catch ex As Exception
      > Throw New MyObjectCreatio nFailedExceptio n(<stuff>)
      > Finally
      > LaunchNuclearMi ssiles()
      > End Try[/color]

      That wasn't what I was asking. I want to run cleanup code ONLY if an
      exception occurs, and I wanted to know if using Throw absolutely, positively
      exits a procedure without running any other statements that follow it
      (Finally blocks notwithstanding ; I know they're ALWAYS executed). Perhaps I
      should have asked it as "Will any more code in a given Catch block run after
      a Throw statement?"


      Comment

      • Charles Law

        #4
        Re: Will code after a Throw ever run?

        Ah, yes, I see what you mean.

        I would not expect any code to execute after the Throw. Could you not just
        execute the clean-up in the Catch block, but before the Throw? Otherwise,
        you would have to use a flag to select the code you want to run in the
        Finally block.

        Charles


        "Jeff Johnson [MVP: VB]" <i.get@enough.s pam> wrote in message
        news:emSSrKXeDH A.1828@TK2MSFTN GP10.phx.gbl...[color=blue]
        >
        > "Charles Law" <blah@thingummy .com> wrote in message
        > news:Oj1DMGXeDH A.2304@TK2MSFTN GP10.phx.gbl...
        >[color=green]
        > > Just one statement missing:
        > >
        > > Try
        > > m = New MyObject
        > > Catch ex As Exception
        > > Throw New MyObjectCreatio nFailedExceptio n(<stuff>)
        > > Finally
        > > LaunchNuclearMi ssiles()
        > > End Try[/color]
        >
        > That wasn't what I was asking. I want to run cleanup code ONLY if an
        > exception occurs, and I wanted to know if using Throw absolutely,[/color]
        positively[color=blue]
        > exits a procedure without running any other statements that follow it
        > (Finally blocks notwithstanding ; I know they're ALWAYS executed). Perhaps[/color]
        I[color=blue]
        > should have asked it as "Will any more code in a given Catch block run[/color]
        after[color=blue]
        > a Throw statement?"
        >
        >[/color]


        Comment

        • Jeff Johnson [MVP: VB]

          #5
          Re: Will code after a Throw ever run?


          "Charles Law" <blah@thingummy .com> wrote in message
          news:ubEGKWXeDH A.1832@TK2MSFTN GP09.phx.gbl...
          [color=blue]
          > Ah, yes, I see what you mean.
          >
          > I would not expect any code to execute after the Throw. Could you not just
          > execute the clean-up in the Catch block, but before the Throw?[/color]

          Yes, and I rearranged my code to do so. I just cannot find concrete
          documentation that says that Throw forces (i.e., guarantees) an exit from a
          procedure.


          Comment

          • Tom Shelton

            #6
            Re: Will code after a Throw ever run?

            Jeff Johnson [MVP: VB] wrote:
            [color=blue]
            >
            > "Charles Law" <blah@thingummy .com> wrote in message
            > news:ubEGKWXeDH A.1832@TK2MSFTN GP09.phx.gbl...
            >[color=green]
            >> Ah, yes, I see what you mean.
            >>
            >> I would not expect any code to execute after the Throw. Could you not
            >> just execute the clean-up in the Catch block, but before the Throw?[/color]
            >
            > Yes, and I rearranged my code to do so. I just cannot find concrete
            > documentation that says that Throw forces (i.e., guarantees) an exit from
            > a procedure.[/color]

            I'm not sure that it is explicitly stated in this manner, but since throwing
            an exception causes an immediate search up the call stack for an error
            handler, it is guarenteed for this to be the case... In other words,
            control will be immediately passed to the next error handler.

            Tom Shelton

            Comment

            • Jeff Johnson [MVP: VB]

              #7
              Re: Will code after a Throw ever run?


              "Tom Shelton" <tom@mtogden.co m> wrote in message
              news:%23OUD8Pce DHA.2408@TK2MSF TNGP09.phx.gbl. ..
              [color=blue]
              > I'm not sure that it is explicitly stated in this manner, but since[/color]
              throwing[color=blue]
              > an exception causes an immediate search up the call stack for an error
              > handler, it is guarenteed for this to be the case... In other words,
              > control will be immediately passed to the next error handler.[/color]

              Right, that I've seen. I just want warm and fuzzies that searching for an
              error handler isn't like calling a subroutine: i.e., that control WON'T
              return to the statement after the Throw once the error has been handled. I'm
              pretty sure it won't (since it hasn't yet), but it's the kind of thing that
              should be stated in the docs.


              Comment

              Working...