473,324 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 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 2286
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
5
by: Mullin Yu | last post by:
i want to build an application of both gui and batch interface by using windows application project. i check either passing any args or not. if no, then open the gui application. if yes, use the...
17
by: MumboJumbo | last post by:
Hi I have a really basic question hopefully some can help me with: Can you write a (i.e. one) C# project that works from the cmd line and gui? I seems if i write a GUI app it can't write to...
7
by: Job Lot | last post by:
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...
6
by: Giojo | last post by:
Hello guys! I can't resolve this problem.. I want my programm in c# working with only console if there are some parameters, but if someone make double-click on the exe I want to start the graphic...
2
by: Jim Heavey | last post by:
Hello, I used VS 2005 to create a console application. Got the application working just fine. I then decided I would create a folder for each of my "console" applications within that project. I...
1
by: John Wright | last post by:
I am running a console application that connects to an Access database (8 million rows) and converts it to a text file and then cleans and compacts the database. When it runs I get the following...
12
by: Dilip | last post by:
Hi All I have a server based C# console application. This application must hide its console window when its launched out on the field. So I dutifully P/Invoke'd FindWindow/ShowWindow...
3
by: Sheikko | last post by:
Sincerly is a little bit complicated to explain to you what I have in my mind, but I will try: Above all the problem is the type of data that I want to passe between these two applications. The...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.