How unhandle exception occur

T.Zacks 3,996 Reputation points
2022-07-17T07:37:08.87+00:00

Please share a c# code which will generate unhandle exception which can not be caught by Try / Catch.

also please discuss how one can capture unhandle exception ?

Thanks

Developer technologies C#
0 comments No comments
{count} vote

Accepted answer
  1. Reza Aghaei 4,986 Reputation points MVP Volunteer Moderator
    2022-07-19T09:33:10.623+00:00

    Unhandled exception is an exception which have not been handled in user code by a try/catch block.

    There are a few exceptions which cannot be caught by a try/catch block in user code. For example: starting with the .NET Framework 2.0, you can't catch a StackOverflowException with a try/catch block.

    You can reproduce it easily by having a function calling itself (which really may happen in a recursive function with no proper ending condition). For example:

    using System;  
      
    namespace temp  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                Main(args); // Oops, this recursion won't stop.  
            }  
        }  
    }  
    

    You can learn more about the example in Debug StackOverflow errors.

    For the exceptions which are not being handled by try/catch in your code, depending to the framework which you are using, you can use the following options:

    If you don't follow any of above mechanism, then you may need to look into Event Viewer to see the unhandled exceptions under Application event, for .NET Runtime or for your application.

    Before you end with an unhandled exception, you may want to take a look at Best practices for exceptions which describes best practices for handling and throwing exceptions.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-07-17T08:31:03.787+00:00

    If this is for Windows Forms, see my NuGet packages KP.ExceptionHandling which is explained and how to use in the following Microsoft article.

    In regards to what can not be caught, can't think of one right now but you can simulate with Environment.FailFast("");

    Basic usage in Program.cs

    using System;  
    using System.Collections.Generic;  
    using System.Diagnostics;  
    using System.Linq;  
    using System.Threading.Tasks;  
    using System.Windows.Forms;  
    using ExceptionHandling;  
      
    namespace KP_ExceptionPackageTest  
    {  
        static class Program  
        {  
            /// <summary>  
            ///  The main entry point for the application.  
            /// </summary>  
            [STAThread]  
            static void Main()  
            {  
      
                Application.SetHighDpiMode(HighDpiMode.SystemAware);  
                Application.EnableVisualStyles();  
                Application.SetCompatibleTextRenderingDefault(false);  
      
                // Handling UI thread exceptions to the event.  
                Application.ThreadException += UnhandledExceptions.Application_ThreadException;  
      
                // For handling non-UI thread exceptions to the event.   
                AppDomain.CurrentDomain.UnhandledException +=  
                    UnhandledExceptions.CurrentDomain_UnhandledException;  
      
                // Indicates capturing exception has completed  
                UnhandledExceptions.OnProcessingCompletedEvent += OnProcessingCompletedEvent;  
      
                // TODO Change this to your startup form  
                Application.Run(new Form1());  
            }  
      
            private static void OnProcessingCompletedEvent()  
            {  
                var f = new AppErrorForm { Text = @"Your title goes here" };  
                f.ShowDialog();  
                Application.Exit();  
            }  
        }  
    }  
    

    Source code which also shows a log viewer

    221520-figure1.png

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.