Re: My application exits after handling an ApplicationException



For the moment, I did the less elegant thing of setting a flag when Cancel
is clicked and testing for it in my loop. But I left the Try/Catch/Finally
code in place.

So what next surprised me was when I messed up a SQL statement and got an
exception from ADO.NET. My code caught the exception, closed its files, and
returned to its message loop, no problem, no early exit.

Yes, right now the code starts running in Module MainModule in a Sub Main,
and it creates the form and runs it. Here's that bit of code:

Public Sub Main()

' Enable XP visual styles
'
Application.EnableVisualStyles()
Application.DoEvents()

' Create and run our form
'
f = New Form1
Application.Run(f)

End Sub

I'll post the code for the exception declaration, the Throw and the
Try/Catch/Finally in my next message ...

Tad

"Adam Goossens" <adam.goossens@xxxxxxxxx> wrote in message
news:%23CKi7ykPFHA.3076@xxxxxxxxxxxxxxxxxxxxxxx
> Hi Tad,
>
> Wow, that must be irritating. Did you start the application message loop
> by passing an instance of your form to Application.Run()? If you did and
> you close the form, the application will terminate.
>
> As long as you've caught the exception, you shouldn't have a problem. The
> only thing I can think of is that there's some form of code there doing
> something it shouldn't be. A logic error, perhaps?
>
> Let me know how you go...this has me intrigued :)
> Regards,
> -Adam.
>
> Tad Marshall wrote:
>> Hi,
>>
>> I'm having limited luck getting an ApplicationException to work right in
>> my code. This is VB.NET, VS 2003, Windows XP SP2, .NET Framework 1.1.
>>
>> I thought it would be convenient to take advantage of exception handling
>> to deal with the case of the user clicking the Cancel button while a long
>> operation was running. So, I declared a class inherited from
>> ApplicationException and I Throw one of these with the text "User pressed
>> Cancel" in the OnClick handler for the Cancel button. I have a
>> Try/Catch/Finally/End Try block around the code that is running, and it
>> all works great. I catch the exception, log a message that the user
>> pressed cancel, then run the Finally block and close all my files, and I
>> return to the OnClick handler for the Start button that began the
>> operation, and it re-enables the Start button and returns from the
>> OnClick handler.
>>
>> Then my program exits, with error code 0.
>>
>> If I take out the Try stuff, VS 2003 catches it and puts up a dialog
>> saying that a UserInterruptException has occured with additional
>> information "User pressed Cancel". I can Break into the debugger and
>> look around, but if I Continue then it is the same as when I had the Try
>> block ... everything gracefully cleans up just as designed, and then the
>> program exits, which is NOT my design.
>>
>> Am I missing something? I've used Structured Exception Handling in
>> Win32, and all I had to do was handle the error and my code would keep
>> running. I thought that was the whole point! You can exit if you want
>> to, but if you can handle the error and continue, then you do that
>> instead.
>>
>> Any suggestions? Thanks!
>>
>> Tad
>>

.