471,354 Members | 1,489 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

Console Application Problem

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

Jul 21 '05 #1
7 2151
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" <Jo****@discussions.microsoft.com> wrote in message
news:E9**********************************@microsof t.com...
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

Jul 21 '05 #2
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:
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" <Jo****@discussions.microsoft.com> wrote in message
news:E9**********************************@microsof t.com...
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


Jul 21 '05 #3
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:
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
'PUT "END" HERE, like this:
End
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

Jul 21 '05 #4
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
Jul 21 '05 #5
If you put end there, the app will exit EVERY time whether there's an
exception or not. Instead of using end, use
Threading.Thread.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.WriteLine(i(3).ToString)
Catch ex As Exception
Console.WriteLine(ex.Message)
Threading.Thread.CurrentThread.Abort
Finally
i = Nothing
End Try
End Sub

Jul 21 '05 #6
Thanks Chris, this is what i was looking for.

"Chris Dunaway" wrote:
If you put end there, the app will exit EVERY time whether there's an
exception or not. Instead of using end, use
Threading.Thread.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.WriteLine(i(3).ToString)
Catch ex As Exception
Console.WriteLine(ex.Message)
Threading.Thread.CurrentThread.Abort
Finally
i = Nothing
End Try
End Sub

Jul 21 '05 #7
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.Abort :)

Chris Dunaway пишет:
If you put end there, the app will exit EVERY time whether there's an
exception or not. Instead of using end, use
Threading.Thread.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.WriteLine(i(3).ToString)
Catch ex As Exception
Console.WriteLine(ex.Message)
Threading.Thread.CurrentThread.Abort
Finally
i = Nothing
End Try
End Sub

Jul 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by MumboJumbo | last post: by
7 posts views Thread by Job Lot | last post: by
6 posts views Thread by Giojo | last post: by
2 posts views Thread by Jim Heavey | last post: by
1 post views Thread by John Wright | last post: by
3 posts views Thread by Sheikko | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.