Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Console Application Problem 1

Status
Not open for further replies.

meinhunna

Programmer
Jul 31, 2004
118
AU
How can I terminate console application in Try…Catch…Finally…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.WriteLine(i(3).ToString)

Catch ex As Exception
Console.WriteLine(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.WriteLine(i(1).ToString)

Catch ex As Exception
Console.WriteLine(ex.Message)

Finally
i = Nothing
End Try
End Sub

End Module
 
Is deleting of "I" is what you're trying to do? If so, you don't have to do that thanks for .NET garbage collector. It does it for you.
If you need the finally for anything else, write:
Code:
   Finally 
      i = Nothing 
      Environment.Exit(1)
   End Try
This will end the colsole application.
 
End statement kills a process, that is why Finally is not getting executed. Use System.Threading.Thread.CurrentThread.Abort(). Finally block will be executed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top