Catch Exceptions Globally and Resume

ayozzhero

Well-known member
Joined
Apr 6, 2005
Messages
186
Location
Malaysia
Programming Experience
1-3
To catch any errors in my app which is not within the Try...Catch...End Try block, I put this in the Sub Main:

VB.NET:
[color=Blue]AddHandler [/color]Application.ThreadException, [color=Blue]AddressOf [/color]OnThreadException

which redirects the error to:

VB.NET:
[color=black][color=Blue]Private Sub[/color] OnThreadException([color=Blue]ByVal [/color]sender [color=Blue]As Object[/color], _
				[color=Blue]ByVal [/color]e [color=Blue]As [/color]ThreadExceptionEventArgs)
	[color=Blue]Try[/color]
		ErrorMessage = "Error Description:" & vbCrLf & e.Exception.Message.ToString & _
				vbCrLf & vbCrLf & _
				"Other Info: " & vbCrLf & _
				e.Exception.ToString & vbCrLf
	[color=Blue]Catch [/color]ex [color=Blue]As Exception[/color]
		ErrorMessage = "Error Description:" & vbCrLf & "Unknown Error"
	[color=Blue]Finally[/color]
		[color=Blue]Dim [/color]oForm [color=Blue]As New[/color] frmErrorCapture
		oForm.Show()
	[color=Blue]End Try[/color]
[color=Blue]End Sub[/color][/color]

I want to trap a specific error, e.g. Divide by Zero. In OnThreadException I will handle the error, i.e. resetting to Zero value to one. Then, I want to return to the same function/code that generates the error and run it back again (this time there should be no more Divide by Zero error because I already reset the variable value). Can I do this in VB .Net?

Thank you for helping.
 
Not that I'm aware of. If an exception is thrown then execution resumes from the place it was caught. You would need to call some global function from the Catch block at the point the exception was thrown to be able to resume from that point.
 
Back
Top