Is there a way to have my VB.NET program perform some sort of "dying breath" action? - vb.net

Is it possible to have some sort of global action/event that triggers in the event of a fatal error? I'd like to be able to have my program write an error file and/or perform other "last breath" (no idea what else to call it...) action if a fatal or unhandled exception occurs that would cause the program to close or crash. Is such a thing possible?
I'm programming in VB.NET v4 using VS2010, if that is important. Also, all users of my app will be authenticated as an administrator (if that matters?).

It depends on what kind of application you're writing, but these may help:
Application.ApplicationExit
Application.ThreadException
AppDomain.UnhandledException

Yes there is AppDomain.UnhandledException Event. And no you do not need any permissions to use it.

Related

Error in 3rd Party Library Causing Hanging in Release

My main program is an ASP.Net Core Web API that has a third party library in a hosted service. The third party library is initializing fine but then it throws some errors sometime throughout its lifecycle.
It supplies a way of hooking into the object via an event and will let me know what the error is so that I can handle it but it still throws in the third party library..
Since I am handling the event myself, I want to completely ignore these errors that are occurring in this library. Is there anyway that I can do that?
I have already tried to add a global exception handler and the strange thing is, this exception handler never gets hit. The only way I can get the exception is to set my exception settings to break when CLR exceptions happen like in the picture above
This does not crash my program. For some reason, the program just hangs. When I turn off CLR exceptions in the "Break when thrown" window, then the program runs just fine. It is almost like visual studio is doing something special to handle these types of exceptions that a console version cannot do
The only way that I can seem to get a console version of this running, is attach a visual studio debugger to the process and when the exception is hit, press the green play button "Continue" in visual studio. Otherwise the application just seems to hang on the exception being thrown by the third party library.
The application will run fine as long as visual studio is attached and the CLR break exceptions are not checked
Does anyone know how to make sure that these types of exceptions do not hang the program when released?
Additional Info:
The third party library is a .NET Framework 4 library
The Asp.Net project is targetting "net5.0-windows"
The 3rd party class is probably using multi-threading
if it helps, this is how I am creating the third party class
Handling NullReferenceException in release code(Official advice)
It's usually better to avoid a NullReferenceException than to handle it after it occurs. Handling an exception can make your code harder to maintain and understand, and can sometimes introduce other bugs. A NullReferenceException is often a non-recoverable error. In these cases, letting the exception stop the app might be the best alternative.
However, there are many situations where handling the error can be useful:
1.Your app can ignore objects that are null. For example, if your app retrieves and processes records in a database, you might be able to ignore some number of bad records that result in null objects. Recording the bad data in a log file or in the application UI might be all you have to do.
2.You can recover from the exception. For example, a call to a web service that returns a reference type might return null if the connection is lost or the connection times out. You can attempt to reestablish the connection and try the call again.
3.You can restore the state of your app to a valid state. For example, you might be performing a multi-step task that requires you to save information to a data store before you call a method that throws a NullReferenceException. If the uninitialized object would corrupt the data record, you can remove the previous data before you close the app.
4.You want to report the exception. For example, if the error was caused by a mistake from the user of your app, you can generate a message to help them supply the correct information. You can also log information about the error to help you fix the problem. Some frameworks, like ASP.NET, have a high-level exception handler that captures all errors to that the app never crashes; in that case, logging the exception might be the only way you can know that it occurs.
So after days of research I've finally found an event to hook into to give you error messages from ANY source no matter how many level deep you go in threads.
AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;
Hooking into this event it will allow you to see errors from every library and every thread. Simply place the above into you program.cs (or whatever startup file you have) and magically you will be flooded with all of the unknown errors from all of the 3rd party libraries you thought were once flawless.
private static void CurrentDomain_FirstChanceException(object sender, System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs e)
{
Console.WriteLine(e.Exception.Message, e.Exception.StackTrace);
}
I've done so with the following method and low and behold. The third party library was trying to reference another project in an unsafe way and throwing an error. Since I didn't need this other project reference the built exe did not have a reference to this assembly because I had no direct reference to it in the project (darn smarty pants who need to optimize everything). I was able to run correctly because in my visual studio solution, I had a reference to this other project. So the third party library would pick up on it as soon as visual studio connected with the debugger through some sort of dark magic.
Anyways, I made a throw away object that used the project that was required and the issue was solved.
I really hope that this helps someone else and saves them the days it took me to find this.

When programming, do you show all errors as message boxes or do you put them in a log file

I am trying to develop a standard when I code applications.
I was curious as to what other developers did when it comes to sql errors or general program errors. Do you output the error to the screen, write to a log file, or something else?
It really depends on the severity of the error.
Is it a show stopper?
Can the software automatically retry and get away with no message?
Can it be ignored?
You can log every exception, or just certain ones, or none. I have a custom Exception class which logs every exception created (of that type).
I have an unhandled exception handler which emails me when there is one.
I'd only send a message to the user when it will change the way the application works from the user's point of view.
Your question is a bit subjective and you would get opinion-based answers if the entire community bothered to answer.
If the error is relevant and important to the user (e.g.: invalid username/password) display it to the user using a message box.
If the error is relevant to the developer, or can be used in the debugging process, use a log or a console output.
The trick is to identify which and how the errors should be displayed to the user. You don't want to bombard the user with exceptions and complicated errors on which the user has no idea on how to act upon.

Avoid system.threading.threadabortexception error

I have a vb.net desktop application and I am using thread.Abort() there.
I am getting system.threading.threadabortexception error.
Below is the error messagebox i am getting but not everytime.
Unhandled exception has occured in your application. If you click
Continue.....
Thread was being aborted.
Please suggest how to avoid this errormessage.
Please suggest how to avoid this error message.
The best, and correct, way to avoid this is to not call Thread.Abort(). Thread.Abort() is really a bad idea in general. Instead, you should build your routines around the cooperative cancellation model built in the framework.

Determine which line causes a vb.net program to crash

I have created a vb.net program and released it to the customer. They are running the exe and at times the program crashes unexpectedly and display the message: "... has encountered a problem and needs to close"
I know I should have added code to handle the exception, but is there a way that I can find out which line in the program caused the error? What is generally as good way to track errors in a program after it has been released?
Thanks
for this kind of i-don't-know-where-to-look issue, i trapped exception at application level with the Application.DispatcherUnhandledException event :
http://msdn.microsoft.com/en-us/library/system.windows.application.dispatcherunhandledexception.aspx
and then in the event handler i get the StackTrace and display it in a MessageBox / dump it in a file, along with the exception.Message.
Next i offer the user the choice to re-launch the application.

Error handling in wxWidgets

Could someone provide info about error-handling in wxWidgets or a pointer to documentation?
Specifically, I discovered this behavior: I try to create a wxImage from a file. This is in an event-handler. The file is not present. The call to the image constructor does not throw an exception. (I understand that no wxWidgets code throws exceptions.) A call to image.Ok() returns false. Fine. But after my event-handler exits, wxWidgets gratuitously pops up an error message dialog. That's okay for this particular application, but I wonder how to stop that from happening if I want to handle the error myself. I suspect that the dialog is coming from an event-handler, but I search for things like EVT_ERROR, and came up empty.
There is the class wxLogNull for suppressing those log messages. See http://docs.wxwidgets.org/stable/wx_wxlognull.html#wxlognull where also an example is given.
Read the wxLog overview for more details on how wxWidgets handles this.
You can define your own log target which would throw an exception if an error message is logged. Of course, then you'd probably need to catch it in your event handler anyhow as you probably don't want to just give the user a relatively useless "File couldn't be opened" message but rather a "Image couldn't be opened" one. And if you do this, then why not just test for file.IsOk() directly and use wxLogError() yourself? IOW you can do what you want but I don't really see how is it better than the traditional exception-less way of doing things in this particular case.