Disable 'continue' option when an unhandled exception occurs?

G

Guest

Hi,

I feel like a noob for asking this.

When I publish a VB windows application, I want to disable the ability of
the the user to continue when there is an unhandled exception.

For example, if there is a bug in the program that causes an exception, I
want the program to crash. If there is an unhandled exception the program is
in an undefined state, and continuing could be dangerous. I'm surprised the
default is to give the user the option to continue, quite frankly. I'm using
Visual Studio 2005.

Thanks,
-SurturZ
 
J

Jeffrey Tan[MSFT]

Hi SurturZ,

Yes, your thought is right. Allowing end user to continue using the
application after unhandled exception is really dangerous.

Net Winform Application.Run() method internally will setup a big
try...catch around all the message pump code to catch any unhandled
exception in the main GUI thread. When an unhandled exception is generated,
the default catch handler will capture the details of this exception and
display them in an error dialog. This dialog contains the button to dismiss
the exception(Continue) or terminate the application(Quit). Note: this
dialog will not pop up for any exceptions generated in other threads.

To change this default behavior, you may hook Application.ThreadException
event and provide a customized unhandled exception handler for main GUI
thread. In this handler, you may log the handled exception for debugging
purpose and them invoke Application.Exit() method to exit the crashing
application. Please refer to the MSDN document of
Application.ThreadException event for sample code of using this event.

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Phill W.

surturz said:
When I publish a VB windows application, I want to disable the ability of
the the user to continue when there is an unhandled exception.

Simple answer - don't leave any exceptions unhandled.
If you do, then control passes over to the user and there's /nothing/
you can do to stop them hitting the "Continue" button.
For example, if there is a bug in the program that causes an exception, I
want the program to crash.

Yes, but not as "far" as that appalling "Oops" dialog.

Add a handler for Application.ThreadException and clean things up in
there. Show the user a sanitised message (in,say, a MessageBox) and
then take the application down completely.

Oh, and watch out for Threads as well; in VB'2005, if an Exception
"escapes" from one of your [background] Threads, it will take the entire
application down with it.
I'm surprised the default is to give the user the option to continue,
quite frankly.

I couldn't agree more. The application will probably be in a really bad
way at this point and to give a user the option to say "Oh well, that
error doesn't matter" is just terrifying.

HTH,
Phill W.
 
G

Guest

Many thanks that is very helpful.

Hopefully the Continue option is removed in Orcas!

Regards,
-SurturZ
 
J

Jeffrey Tan[MSFT]

Hi SurturZ,

Thank you for the feedback.

I am not sure if Winform team will remove this continue button in Orcas.
Actually, the Winform team highly recommended the developer to provide a
customized ThreadException handler, so that they can fully control the
crash logic themselves. The reason that the "Continue" button is on by
default is some developers requested this feature for debugging purpose.

Anyway, if you need any further help or have any concern, please feel free
to tell me, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Johnny Jörgensen

Open the project properties. On the "Application" tab, you click the "View
Application Events" button. That will generate a new partial class looking
like this:

Namespace My

' The following events are availble for MyApplication:

'

' Startup: Raised when the application starts, before the startup form is
created.

' Shutdown: Raised after all application forms are closed. This event is not
raised if the application terminates abnormally.

' UnhandledException: Raised if the application encounters an unhandled
exception.

' StartupNextInstance: Raised when launching a single-instance application
and the application is already active.

' NetworkAvailabilityChanged: Raised when the network connection is
connected or disconnected.

Partial Friend Class MyApplication

End Class

End Namespace


Create a handler like this in the MyApplication class:

Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e
As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs)
Handles Me.UnhandledException

'My.Application.Log.WriteException(e.Exception, TraceEventType.Critical,
"Unhandled Exception.")

MsgBox("An unhandled exception occurred: " & e.Exception.Message &
vbCrLf & vbCrLf & "I'm going to quit now!")

End

End Sub

That should do it.

Cheers,
Johnny J.
 
K

Kevin S Gallagher

UnhandledExceptionEventArgs.ExitApplication Property in ApplicationEvents.vb
under VS2005 gives you the ability to not continue

Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e
As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs)
Handles Me.UnhandledException

' Do your recovery like write to a log file followed by

e.ExitApplication = True

End Sub
 
J

Jeffrey Tan[MSFT]

Hi SurturZ,

Have you reviewed my last reply to you? If you still have anything unclear,
please feel free to tell me, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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