when ever my program is popping 'UnHandled Exception', can I call for a button click in my program? I have a button in my form which can actually 'fix' or 'go around' the exception. like, something 'On Error Button2.PerformClick() 3 times' ( just an example of my thinking )
Asked
Active
Viewed 630 times
-1
-
2The question is unclear. What code, what button, what loop? The only advice one can give is to put the code in an [try/catch block](https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/try-catch-finally-statement). This is adequately covered in the documentation and all VB.NET tutorials. Google for `.NET Exception Handling`. – Panagiotis Kanavos Aug 30 '17 at 13:54
-
I just edited my post, Might be more clear now.. – Shlomi Aug 30 '17 at 14:05
-
If you want to do something when there is an exception, you need to handle the exception. Use a Try/Catch block to do that. – Blackwood Aug 30 '17 at 14:15
-
Alternatively if you would like to bypass all exceptions (Not Recommended) you can try subscribe to the `Application.ThreadException` event :) – Sasha Aug 30 '17 at 14:21
-
@Shlomi once the exception is thrown and is not handled, the program already crashed. You may have a try/catch block that whenever the exception is caught notifies the user and asks him for an input (e.g. like you said pressing a button) – Luca Mozzo Aug 30 '17 at 14:21
-
@Shlomi did you *read* the linked documentation? It already contains an example. – Panagiotis Kanavos Aug 30 '17 at 14:47
-
See [this C# answer](https://stackoverflow.com/a/6292001/832052), but it's a little more straightforward in c# because you have access to this code in Program.cs: `Application.Run(new Form1());` which starts the message loop (UI thread). You must subscribe to the exception event handler *before* starting the message loop, which is not possible in vb.net out of the box... See [this vb.net answer](https://stackoverflow.com/a/2862024/832052) if you want to use `Application.Run(Form)` in vb.net. Then you can set up your handler beforehand, and even wrap the call in a `Try...Catch` itself. – djv Aug 30 '17 at 16:30
1 Answers
0
Enable the application event and handle it from there, You can use the unhandled exception function to show your form, Take a look at the below code:
Imports System.Text
Imports System.IO
Namespace My
' The following events are available 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
'One of the global exceptions we are catching is not thread safe,
'so we need to make it thread safe first.
Private Delegate Sub SafeApplicationThreadException(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs)
Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
'There are three places to catch all global unhandled exceptions:
'AppDomain.CurrentDomain.UnhandledException event.
'System.Windows.Forms.Application.ThreadException event.
'MyApplication.UnhandledException event.
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf AppDomain_UnhandledException
AddHandler System.Windows.Forms.Application.ThreadException, AddressOf app_ThreadException
' AddHandler AccessViolationException, AddressOf app_AccessViolationException
End Sub
'Private Sub app_AccessViolationException(ByVal sender As Object, ByVal ex As System.AccessViolationException)
'End Sub
Private Sub app_ThreadException(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs)
'This is not thread safe, so make it thread safe.
If MainForm.InvokeRequired Then
' Invoke back to the main thread
MainForm.Invoke(New SafeApplicationThreadException(AddressOf app_ThreadException), _
New Object() {sender, e})
Else
frmDebug.Show()
End If
End Sub
Private Sub AppDomain_UnhandledException(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
frmDebug.Show()
End Sub
Private Sub MyApplication_UnhandledException(sender As Object, e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
frmDebug.Show()
End Sub
End Class
End Namespace

M. Waheed
- 877
- 1
- 8
- 8