Console Application Problem

G

Guest

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
 
S

Shuvro Mazumder [MSFT]

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
 
G

Guest

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
 
V

Vasya Marchuk

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 said:
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
 
C

Cor Ligthert

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
 
C

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
 
V

Vasya Marchuk

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 пишет:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top