473,772 Members | 2,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Jul 21 '05 #1
7 2327
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****@discuss ions.microsoft. com> wrote in message
news:E9******** *************** ***********@mic rosoft.com...
How can I terminate console application in Try.Catch.Final ly.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

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****@discuss ions.microsoft. com> wrote in message
news:E9******** *************** ***********@mic rosoft.com...
How can I terminate console application in Try.Catch.Final ly.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


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.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
'PUT "END" HERE, like this:
End
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

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.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

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.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

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.A bort :)

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.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

Jul 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5387
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 application (VB.NET) will start a process, redirect its stdout and capture that process' output, displaying it in a window. I've written a component for this, and a test application for the component. It allows me to specify a command to execute,...
5
4053
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 batch interface. my problem is that i can output the string to the dos prompt when running the exe file at dos prompt: c:\>test.exe /?
17
4235
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 console using System.Console.WriteLine if thge project has its "Output Type" to "Windows Application". However I can write to stdio if i set output type to "Console Application". When I do this I unfortunately get a "console box" as well
7
399
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 written in Console Application. Module Module1 Sub Main() Call Testing()
6
10434
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 GUI without the black console on background.. Now I check if there are some parameters and if there aren't parameters I run the GUI (with the black console that remain..). If I choose Windows application on Visual Studio, when I run the...
2
525
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 created a new folder and placed the working application under tht folder (This included the .config file, the driver .cs (void main) and the class which is called by the driver class which does all of the work). I created a new folder for a new...
1
2043
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 error: The CLR has been unable to transition from COM context 0x1a2008 to COM context 0x1a2178 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long...
12
6543
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 combination to hide the console window at launch time. The application (for legacy reasons) hangs around by waiting on an old- fashioned Console.ReadLine() statement.
3
2243
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 complete application that I want to create consiste of two applications: - the console developped with VS C++, that use some libraries developped with Borland C++ - the GUI of application that interact with the console.
0
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9911
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8934
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.