NET Runtime error and another more generic Windows Application Error.įrom the. Windows Event Viewer may log 2 different entries for the same exception. This can be helpful if you can’t figure out why your application suddenly crashes. If your application has unhandled exceptions, that may be logged in the Windows Event Viewer under the category of “Application”. MORE: AppDomain.UnhandledException Event (MSDN) View Unhandled Exceptions in Windows Event Viewer Static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)ĭebug.WriteLine((e.ExceptionObject as Exception).Message)
Static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) static void Main(string args)Īpplication.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException) Ī += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException)
#Sct device updater unhandled exception code#
For Windows applications, it could be the first couple lines of code in the Main() method. For ASP.NET, you would do this in the Startup class or Global.asax. You only need to register for these events once in your code when your application starts up. NET Framework provides a couple events that can be used to catch unhandled exceptions.
#Sct device updater unhandled exception how to#
MORE: C# Exception Handling Best Practices How to Catch Unhandled Exceptions in C# This code can easily throw several types of exceptions and lacks exception handling best practices. String fileContents = File.ReadAllText(args) This code will throw exceptions if no file path is passed in or the file does not exist. The code then loads the contents of the file path being passed in.
The developer is assuming that within “args” a valid file path will be passed in. This is a simple example of a potential known problem that is accounted for within the code.Īn unhandled exception occurs when a developer does not anticipate and handle a potential exception NET Framework will then throw a FileNotFoundException. An unhandled exception occurs when the application code does not properly handle exceptions.įor example, When you try to open a file on disk, it is a common problem for the file to not exist.