473,387 Members | 1,575 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,387 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 2287
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.