Catch Application Exceptions in a Windows Forms Application
In Windows Forms applications, when an exception is thrown anywhere in the application (on the main thread or during asynchronous calls), you can catch it by registering for the ThreadException event on the Application. In this way you can treat all the exceptions in the same way.
Application.ThreadException += new ThreadExceptionEventHandler(MyCommonExceptionHandlingMethod);
private static void MyCommonExceptionHandlingMethod(object sender, ThreadExceptionEventArgs t)
{
//Exception handling...
}
Create Windows Form with Exception information on catch?
Sure, just create any old form, and display it using ShowDialog
at that point. You can put whatever you want on it. You could then put various buttons on it for "continue", "restart", "quit", etc. You can then inspect a property of the form after ShowDialog
returns to determine what to do, based on the button clicked.
Windows form application exception
I'm guessing that you have bound a List that is initially empty, (or other sort of collection that does not generate list changed events) to your DataGridView
, and then added items to this List.
The items you add will display correctly on your grid, but clicking on a row will cause this exception. This is because the underlying CurrencyManager
will be reporting its current row position as an offset of -1. It will stay this way because the List does not report changes to the grid.
You should only bind your list to the grid if it has some items in it to begin with, or rebind when you add them.
See also my answer to this question, which is essentially the same problem.
WinForms Global Exception Handling?
If #26
is an exception then you can use AppDomain.CurrentDomain.UnhandledException
event. If it's just a return value, then I don't see any chance to handle that globally.
public static void Main()
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
// start main thread here
}
static void MyHandler(object sender, UnhandledExceptionEventArgs args)
{
Exception e = (Exception) args.ExceptionObject;
Console.WriteLine("MyHandler caught : " + e.Message);
}
Related Topics
Onchange Event for HTML.Dropdownlist
Publish .Net Core App as Portable Executable
Maxlength Attribute Not Generating Client-Side Validation Attributes
How to Get The Checkboxlist Selected Values, What I Have Doesn't Seem to Work C#.Net/Visualwebpart
SQL Injections with Replace Single-Quotation and Validate Integers
File Exists by File Name Pattern
Escaping the Escape Character Does Not Work - SQL Like Operator
Fixing Gridview Header While Scrolling
Deserialize JSON to Anonymous Object
How to Fix Ill-Formed HTML with HTML Agility Pack
Reading Data from CSV to Screen Output
Using Regex to Get Text Between Multiple HTML Tags
Scope_Identity() Always Returning 0
Block Request for Multiple Unsuccessful Logins for a Period of Time