Console Application Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Job Lot

    Console Application Problem

    How can I terminate console application in Try…Catch…F inally…End Try block so
    that code in Finally gets executed. If I use End statement Finally does not
    get executed.

    Following is my code written in Console Application.

    Module Module1

    Sub Main()
    Call Testing()

    Call Testing2()
    End Sub


    Private Sub Testing()

    Dim i() As Integer = {1, 2, 3}

    Try
    ‘This will cause exception
    Console.WriteLi ne(i(3).ToStrin g)

    Catch ex As Exception
    Console.WriteLi ne(ex.Message)

    ‘I WANT CONSOLE TO BE TERMINATED HERE SO THAT FINALLY GET EXECUTED. IF I USE
    END STATEMENT HERE FINALLY DOES NOT GET EXECUTED.
    Finally
    i = Nothing
    End Try
    End Sub


    Private Sub Testing2()

    Dim i() As Integer = {1, 2, 3}

    Try
    Console.WriteLi ne(i(1).ToStrin g)

    Catch ex As Exception
    Console.WriteLi ne(ex.Message)

    Finally
    i = Nothing
    End Try
    End Sub

    End Module

  • Shuvro Mazumder [MSFT]

    #2
    Re: Console Application Problem

    I believe that if the try block throws an exception, it'll run the catch
    block code and then run the finally code. The finally code will also run if
    you do not except, after the try block.

    Why are you using an end statement in the catch block?
    - Shuvro
    --
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Use of included script samples are subject to the terms specified at
    http://www.microsoft.com/info/cpyright.htm.



    "Job Lot" <JobLot@discuss ions.microsoft. com> wrote in message
    news:E962EFDA-A2D5-465B-87C9-EF305568A079@mi crosoft.com...[color=blue]
    > How can I terminate console application in Try.Catch.Final ly.End Try block[/color]
    so[color=blue]
    > that code in Finally gets executed. If I use End statement Finally does[/color]
    not[color=blue]
    > get executed.
    >
    > Following is my code written in Console Application.
    >
    > Module Module1
    >
    > Sub Main()
    > Call Testing()
    >
    > Call Testing2()
    > End Sub
    >
    >
    > Private Sub Testing()
    >
    > Dim i() As Integer = {1, 2, 3}
    >
    > Try
    > 'This will cause exception
    > Console.WriteLi ne(i(3).ToStrin g)
    >
    > Catch ex As Exception
    > Console.WriteLi ne(ex.Message)
    >
    > 'I WANT CONSOLE TO BE TERMINATED HERE SO THAT FINALLY GET EXECUTED. IF I[/color]
    USE[color=blue]
    > END STATEMENT HERE FINALLY DOES NOT GET EXECUTED.
    > Finally
    > i = Nothing
    > End Try
    > End Sub
    >
    >
    > Private Sub Testing2()
    >
    > Dim i() As Integer = {1, 2, 3}
    >
    > Try
    > Console.WriteLi ne(i(1).ToStrin g)
    >
    > Catch ex As Exception
    > Console.WriteLi ne(ex.Message)
    >
    > Finally
    > i = Nothing
    > End Try
    > End Sub
    >
    > End Module
    >[/color]


    Comment

    • Job Lot

      #3
      Re: Console Application Problem

      Ok I am automating Outlook from Console Application, which perform number of
      actions on the Indox folder like reading new mails, extracting attachment
      from new mail, saving attachment to specified path, extracting values from
      the saved file, writing values to text file and uploading text file on FTP
      server. Now want to terminate the application if any of these processes fails
      and log off from outlook and close outlook application. If I don’t use End
      statement, it catches the exception show’s it no console and continue with
      next process, whose object variables depends on values from the failed
      process. In that case the next process might throw another exception.

      I want to end the application in case of error, but before that log off and
      close outlook. I can easily do it in win form application, by simply using
      Me.Dispose in Catch, which does execute Finally before close the application.
      But End simple terminates the app without executing Finally.

      I hope this make sense


      "Shuvro Mazumder [MSFT]" wrote:
      [color=blue]
      > I believe that if the try block throws an exception, it'll run the catch
      > block code and then run the finally code. The finally code will also run if
      > you do not except, after the try block.
      >
      > Why are you using an end statement in the catch block?
      > - Shuvro
      > --
      > This posting is provided "AS IS" with no warranties, and confers no rights.
      > Use of included script samples are subject to the terms specified at
      > http://www.microsoft.com/info/cpyright.htm.
      >
      >
      >
      > "Job Lot" <JobLot@discuss ions.microsoft. com> wrote in message
      > news:E962EFDA-A2D5-465B-87C9-EF305568A079@mi crosoft.com...[color=green]
      > > How can I terminate console application in Try.Catch.Final ly.End Try block[/color]
      > so[color=green]
      > > that code in Finally gets executed. If I use End statement Finally does[/color]
      > not[color=green]
      > > get executed.
      > >
      > > Following is my code written in Console Application.
      > >
      > > Module Module1
      > >
      > > Sub Main()
      > > Call Testing()
      > >
      > > Call Testing2()
      > > End Sub
      > >
      > >
      > > Private Sub Testing()
      > >
      > > Dim i() As Integer = {1, 2, 3}
      > >
      > > Try
      > > 'This will cause exception
      > > Console.WriteLi ne(i(3).ToStrin g)
      > >
      > > Catch ex As Exception
      > > Console.WriteLi ne(ex.Message)
      > >
      > > 'I WANT CONSOLE TO BE TERMINATED HERE SO THAT FINALLY GET EXECUTED. IF I[/color]
      > USE[color=green]
      > > END STATEMENT HERE FINALLY DOES NOT GET EXECUTED.
      > > Finally
      > > i = Nothing
      > > End Try
      > > End Sub
      > >
      > >
      > > Private Sub Testing2()
      > >
      > > Dim i() As Integer = {1, 2, 3}
      > >
      > > Try
      > > Console.WriteLi ne(i(1).ToStrin g)
      > >
      > > Catch ex As Exception
      > > Console.WriteLi ne(ex.Message)
      > >
      > > Finally
      > > i = Nothing
      > > End Try
      > > End Sub
      > >
      > > End Module
      > >[/color]
      >
      >
      >[/color]

      Comment

      • Vasya Marchuk

        #4
        Re: Console Application Problem

        Hi Job Lot:

        You must put "End" after Finally and your "i=nothing" , Finally gets
        executed after an exception has been processed by your code.

        See below in the code:

        Job Lot writes:
        [color=blue]
        > Following is my code written in Console Application.
        >
        > Module Module1
        >
        > Sub Main()
        > Call Testing()
        >
        > Call Testing2()
        > End Sub
        >
        >
        > Private Sub Testing()
        >
        > Dim i() As Integer = {1, 2, 3}
        >
        > Try
        > ‘This will cause exception
        > Console.WriteLi ne(i(3).ToStrin g)
        >
        > Catch ex As Exception
        > Console.WriteLi ne(ex.Message)
        >
        > ‘I WANT CONSOLE TO BE TERMINATED HERE SO THAT FINALLY GET EXECUTED. IF I USE
        > END STATEMENT HERE FINALLY DOES NOT GET EXECUTED.
        > Finally
        > i = Nothing[/color]

        'PUT "END" HERE, like this:
        End
        [color=blue]
        > End Try
        > End Sub
        >
        >
        > Private Sub Testing2()
        >
        > Dim i() As Integer = {1, 2, 3}
        >
        > Try
        > Console.WriteLi ne(i(1).ToStrin g)
        >
        > Catch ex As Exception
        > Console.WriteLi ne(ex.Message)
        >
        > Finally
        > i = Nothing
        > End Try
        > End Sub
        >
        > End Module
        >[/color]

        Comment

        • Cor Ligthert

          #5
          Re: Console Application Problem

          Job,

          I did not see the "End" statement in your code.

          However the End statement kills a process. It is the same as with power down
          your computer.
          The finally is not done after that.

          For me it is a code as "Stop" what can be usefull in debugging however
          should never be in a real production environment (Although I have not found
          any usefull situation where to use End)

          I hope this helps?

          Cor


          Comment

          • Chris Dunaway

            #6
            Re: Console Application Problem

            If you put end there, the app will exit EVERY time whether there's an
            exception or not. Instead of using end, use
            Threading.Threa d.CurrentThread .Abort. I tested this and the finally
            block runs in this case.

            Private Sub Testing()

            Dim i() As Integer = {1, 2, 3}

            Try
            'This will cause exception
            Console.WriteLi ne(i(3).ToStrin g)
            Catch ex As Exception
            Console.WriteLi ne(ex.Message)
            Threading.Threa d.CurrentThread .Abort
            Finally
            i = Nothing
            End Try
            End Sub

            Comment

            • Job Lot

              #7
              Re: Console Application Problem

              Thanks Chris, this is what i was looking for.

              "Chris Dunaway" wrote:
              [color=blue]
              > If you put end there, the app will exit EVERY time whether there's an
              > exception or not. Instead of using end, use
              > Threading.Threa d.CurrentThread .Abort. I tested this and the finally
              > block runs in this case.
              >
              > Private Sub Testing()
              >
              > Dim i() As Integer = {1, 2, 3}
              >
              > Try
              > 'This will cause exception
              > Console.WriteLi ne(i(3).ToStrin g)
              > Catch ex As Exception
              > Console.WriteLi ne(ex.Message)
              > Threading.Threa d.CurrentThread .Abort
              > Finally
              > i = Nothing
              > End Try
              > End Sub
              >
              >[/color]

              Comment

              • Vasya Marchuk

                #8
                Re: Console Application Problem

                Right. Though in that case it was the end of the code anyway, so I
                didn't think about the case with no errors :).
                Actually, given only the specified code and nothing else, it would still
                exit at that point even with no End or CurrentThread.A bort :)

                Chris Dunaway пишет:[color=blue]
                > If you put end there, the app will exit EVERY time whether there's an
                > exception or not. Instead of using end, use
                > Threading.Threa d.CurrentThread .Abort. I tested this and the finally
                > block runs in this case.
                >
                > Private Sub Testing()
                >
                > Dim i() As Integer = {1, 2, 3}
                >
                > Try
                > 'This will cause exception
                > Console.WriteLi ne(i(3).ToStrin g)
                > Catch ex As Exception
                > Console.WriteLi ne(ex.Message)
                > Threading.Threa d.CurrentThread .Abort
                > Finally
                > i = Nothing
                > End Try
                > End Sub
                >[/color]

                Comment

                Working...