Handle global exceptions in VB - vb.net

Hello I have this project experiencing some problems with what is supposed to be my codes for the "problem" handler.
Public Event UnhandledException As UnhandledExceptionEventHandler
Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
AddHandler currentDomain.UnhandledException, AddressOf MyHandler
End Sub
Sub MyHandler(ByVal sender As Object, ByVal args As UnhandledExceptionEventArgs)
Dim e As Exception = DirectCast(args.ExceptionObject, Exception)
Using sw As New StreamWriter(File.Open(myFilePath, FileMode.Append))
sw.WriteLine(Date.now & e.toString)
End Using
MessageBox.Show("An unexcpected error occured. Application will be terminated.")
Application.Exit()
End Sub
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
Throw New Exception("Dummy Error")
End Sub
I'm trying to globally catch all exceptions and create logfile during runtime, which works fine in the debugger(exception handling and textfile writing) but cannot catch any unhandled exceptions after I build it in the setup project and Installed into a machine. What am I missing? Do I need to include additional components to my setup project? Help would be greatly appreciated

There is already a way to handle exceptions for the entire application. Embedding the handler in a form means they would only be caught and logged if and while that form was open.
Go to Project -> Properties -> Application and click the "View Application Events" button at/near the bottom.
This will open ApplicationEvents.vb.
Select (MyApplicationEvents) in the left menu; and UnhandledException in the right. This opens an otherwise typical event handler to which you can add code:
Private Sub MyApplication_UnhandledException(sender As Object,
e As ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
Dim myFilePath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
"badjuju.log")
Using sw As New StreamWriter(File.Open(myFilePath, FileMode.Append))
sw.WriteLine(DateTime.Now)
sw.WriteLine(e.Exception.Message)
End Using
MessageBox.Show("An unexcpected error occured. Application will be terminated.")
End
End Sub
This will not catch exceptions while the IDE is running because VS catches them first so you can see them and fix them.

Related

Cannot access a disposed object in VB.NET

Having a small issue.
I am receiving a Cannot access a disposed object error for Form1
Upon clicking a menu item on the main form - the below Sub is called, which opens another form Form1
Private Sub ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem.Click
If (Not Form1.Visible) Then
Form1.Show(Me)
End If
End Sub
Within Form1, there is a Try block. If it isn't passed, Form1 should show a message box, before closing down. The message appears, but it's then that I receive the error (where it says Form1.Show(Me))
Private Sub Form1_Load(ByVal sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Try
'DO STUFF
Catch
MsgBox("Error loading in data. Please contact an administrator")
Me.Close()
Return
End Try
End Sub
I am quite new to this type of programming, and struggling to fix the problem even after searching similar problems. Could someone please assist or point me in the right direction?
EDIT: So looks like this is due to trying to close the form during the Load event. So my question now is, are there any simple alternatives? I've found ways of doing this in C#, but not a lot for vb.net
Here is one option:
Private loadFailed As Boolean = False
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
'...
Catch ex As Exception
loadFailed = True
End Try
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
If loadFailed Then
MessageBox.Show("Load failed")
Close()
End If
End Sub
In that case, the form will show with the message over it, then it will close when the message is dismissed. Here's an option that will not display the form:
Friend Module Form1Manager
Public Sub ShowForm1(owner As Form)
If Not Form1.Visible Then
Try
'...
'Pass data to Form1 here.
Form1.Show(owner)
Catch ex As Exception
MessageBox.Show("Load failed")
End Try
End If
End Sub
End Module
and, to use that:
Private Sub ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem.Click
Form1Manager.ShowForm1(Me)
End Sub

How to debug unhandle exception handling

My current install of Visual Studio 2015 will not allow me to throw an unhandled exception while running code from the IDE. I want to exercise my unhandled exception code but my code:
Private Sub btnTest_Click(sender As System.Object, e As System.EventArgs) Handles btnTest.Click
Throw New System.Exception("An unhandled test exception has occurred.")
End Sub
only works during a normal runtime, not when the code is executed in the IDE.
How can I debug my unhandled exception code in the IDE?
I looked in Debug, Windows, Exception Settings but I don't see a way to do what I want to do. Is there another more global setting that will allow an unhandled exception without the IDE capturing the exception?
I'm using the ApplicationsEvents.vb to hook the event:
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
Private Sub MyApplication_UnhandledException(sender As Object, e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
ExpLog.LogUnhandledException(e, sender)
e.ExitApplication = Not ExpLog.InformUser
End Sub
End Class
End Namespace
Resolution was to create a test stub that exercised the code that was called by the handler:
Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles btnMisc_Throw.Click
If ExpLog.InformUser() Then
MsgBox("Continue")
Else
MsgBox("End Program")
End If
ExpLog.LogMsgBox(New System.Exception("test unhandled exception"), "test LogMsgBox()",,, "programmer note")
End Sub
This doesn't allow for testing the handler but it does exercise the code the handler calls. Looking at some old comments I figured this out five+ years ago... :(
Add a handler to AppDomain.CurrentDomain.UnhandledException
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf UnhandledExceptionHandler
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Throw New System.Exception("An unhandled test exception has occurred.")
End Sub
Public Shared Sub UnhandledExceptionHandler(ByVal sender As Object, ByVal args As UnhandledExceptionEventArgs)
MessageBox.Show(CType(args.ExceptionObject, Exception).Message)
End Sub

VB.NET How do i use ApplicationEvents to catch an exception within a thread?

I have a piece of code in my ApplicationEvents.vb that is supposed to handle all exceptions:
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
Private Delegate Sub SafeApplicationThreadException(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs)
Private Sub ShowDebugOutput(ByVal ex As Exception)
Dim frmD As New frmDebug()
frmD.rtfError.AppendText(ex.ToString())
frmD.ShowDialog()
Environment.Exit(0)
End Sub
Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
System.Windows.Forms.Application.SetUnhandledExceptionMode(UnhandledExceptionMode.Automatic)
AddHandler System.Windows.Forms.Application.ThreadException, AddressOf app_ThreadException
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf AppDomain_UnhandledException
End Sub
Private Sub app_ThreadException(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs)
If MainForm.InvokeRequired Then
MainForm.Invoke(New SafeApplicationThreadException(AddressOf app_ThreadException), New Object() {sender, e})
Else
ShowDebugOutput(e.Exception)
End If
End Sub
Private Sub AppDomain_UnhandledException(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
ShowDebugOutput(DirectCast(e.ExceptionObject, Exception))
End Sub
Private Sub MyApplication_UnhandledException(sender As Object, e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
ShowDebugOutput(e.Exception)
End Sub
End Class
End Namespace
However, i have found that it will not catch exceptions from within, for example, a BGWorker_DoWork sub.
It will, however, catch exceptions on the same BGWorker_RunWorkerCompleted sub.
Is there a way for me to catch the exceptions from within the thread, without using Try-Catch?

Visual Basic- How do i Display a message box when a trigger in oracle is fired

Okay so I am using visual basic as a user interface for my oracle database. I have triggers implemented into oracle already. I want to show that my triggers work in visual basic so I am inserting data that will cause the database to fail and the triggers to be fired up. In visual basic, it just crashes, instead I want a messagebox to display instead of it crashing. How would I do this?
Private Sub TRANSACTIONBindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles TRANSACTIONBindingNavigatorSaveItem.Click
Me.Validate()
Me.TRANSACTIONBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.DataSet)
the line below this is the one that is it doesn't like
Me.TableAdapterManager.UpdateAll(Me.DataSet)
Here is what I Have now? It doesn't like the word Exception though
Private Sub TRANSACTIONBindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles TRANSACTIONBindingNavigatorSaveItem.Click
Me.Validate()
Me.TRANSACTIONBindingSource.EndEdit()
Try
Catch exception
MessageBox.Show("Error", "Error")
End Try
Me.TableAdapterManager.UpdateAll(Me.DataSet)
End Sub
You're doing it wrong, please read the MSDN properly.
Private Sub TRANSACTIONBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TRANSACTIONBindingNavigatorSaveItem.Click
Try
Me.Validate()
Me.TRANSACTIONBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.DataSet)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

Read/Compile vb.net code dynamically from text file

I am trying to read and compile code from a text file. I have done some of its portion but feeling difficulties when trying to add controls to the Form. Kindly guide me. I am attaching code.
Public Class Form1
Sub Execute()
' Creates object of the compiler
Dim objCodeCompiler As System.CodeDom.Compiler.ICodeCompiler = New VBCodeProvider().CreateCompiler
'References/Parameters.
Dim objCompilerParameters As New System.CodeDom.Compiler.CompilerParameters()
objCompilerParameters.ReferencedAssemblies.Add("System.dll")
objCompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll")
objCompilerParameters.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll")
'Compiles in memory.
objCompilerParameters.GenerateInMemory = True
'Runs the source code.
'You can use resources, textbox's or even the settings, up to you! :D
'Dim strCode As String = TextBox1.Text
'Compiler Results
'Dim objCompileResults As System.CodeDom.Compiler.CompilerResults = objCodeCompiler.CompileAssemblyFromSource(objCompilerParameters, strCode)
Dim objCompileResults As System.CodeDom.Compiler.CompilerResults = objCodeCompiler.CompileAssemblyFromFile(objCompilerParameters, "H:\VB project\LE21 - CodeDom - Run code from Textbox\LE21 - CodeDom - Run code from Textbox\LE21 - CodeDom - Run code from Textbox\file.txt")
'If an Error occurs
If objCompileResults.Errors.HasErrors Then
MsgBox("Error: Line>" & objCompileResults.Errors(0).Line.ToString & ", " & objCompileResults.Errors(0).ErrorText)
Exit Sub
End If
'Creates assembly
Dim objAssembly As System.Reflection.Assembly = objCompileResults.CompiledAssembly
Dim objTheClass As Object = objAssembly.CreateInstance("MainClass")
If objTheClass Is Nothing Then
MsgBox("Can't load class...")
Exit Sub
End If
'Trys to excute
Try
objTheClass.GetType.InvokeMember("ExecuteCode",
System.Reflection.BindingFlags.InvokeMethod, Nothing, objTheClass, Nothing)
Catch ex As Exception
MsgBox("Error:" & ex.Message)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Runs the source code from textbox1.
Execute()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim p As New Form2
p.Text = "hahahahah"
p.Show()
End Sub
End Class
"Type Form2 is not defined"
That's because you haven't defined it. You may be dynamically loading and compiling some file which contains code which does define it. But the compiler has no way of knowing that at compile time. Being a statically typed system, the compiler needs to know about all of the types you're using when it compiles the code.
If you're dynamically loading class definitions from a file, then you also need to dynamically invoke those class definitions. That's going to involve a lot of reflection and CodeDOM and whatnot. It's not going to be pretty, and it's definitely not going to have compile-time type checking. (And it's going to be very "stringly typed" in the sense that instead of creating an instance of Form2 you're going to be requesting from reflection to create an instance of "Form2" and hoping for the best.) So you're going to want to put in a lot of error handling for things like, for example, classes which don't exist.