Retrace will reach End of Life on March 31, 2027. Click here to learn more.

What is an Unhandled Exception and How to Find Them

By: mwatson
  |  April 14, 2017
Unhandled exception featured image

What is an Unhandled Exception?

An exception is a known type of error. An unhandled exception occurs when the application code does not properly handle exceptions.

For example, When you try to open a file on disk, it is a common problem for the file to not exist. The .NET Framework will then throw a FileNotFoundException. This is a simple example of a potential known problem that is accounted for within the code.

An unhandled exception occurs when a developer does not anticipate and handle a potential exception

Let’s take this code sample below. The developer is assuming that within “args” a valid file path will be passed in. The code then loads the contents of the file path being passed in. This code will throw exceptions if no file path is passed in or the file does not exist. This would cause unhandled exceptions.

static void Main(string[] args)
{
    string fileContents = File.ReadAllText(args[0]);

    //do something with the file contents
}

This code can easily throw several types of exceptions and lacks exception handling best practices.

MORE: C# Exception Handling Best Practices

How to Catch Unhandled Exceptions in C#

The .NET Framework provides a couple events that can be used to catch unhandled exceptions. You only need to register for these events once in your code when your application starts up. For ASP.NET, you would do this in the Startup class or Global.asax. For Windows applications, it could be the first couple lines of code in the Main() method.

static void Main(string[] args)
{
  Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
  AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

  string fileContents = File.ReadAllText(args[0]);
  //do something with the file contents
}

static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
  // Log the exception, display it, etc
  Debug.WriteLine(e.Exception.Message);
}

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
  // Log the exception, display it, etc
  Debug.WriteLine((e.ExceptionObject as Exception).Message);
}

MORE: AppDomain.UnhandledException Event (MSDN)

View Unhandled Exceptions in Windows Event Viewer

If your application has unhandled exceptions, that may be logged in the Windows Event Viewer under the category of “Application”. This can be helpful if you can’t figure out why your application suddenly crashes.

Windows Event Viewer may log 2 different entries for the same exception. One with a  .NET Runtime error and another more generic Windows Application Error.

From the .NET Runtime:

Application: Log4netTutorial.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IndexOutOfRangeException
   at Log4netTutorial.Program.Main(System.String[])

Logged under Application Error:

Faulting application name: Log4netTutorial.exe, version: 1.0.0.0, time stamp: 0x58f0ea6b
Faulting module name: KERNELBASE.dll, version: 10.0.14393.953, time stamp: 0x58ba586d
Exception code: 0xe0434352
Fault offset: 0x000da882
Faulting process id: 0x4c94
Faulting application start time: 0x01d2b533b3d60c50
Faulting application path: C:\Users\matt\Documents\Visual Studio 2015\Projects\Log4netTutorial\bin\Debug\Log4netTutorial.exe
Faulting module path: C:\WINDOWS\System32\KERNELBASE.dll
Report Id: 86c5f5b9-9d0f-4fc4-a860-9457b90c2068
Faulting package full name: 
Faulting package-relative application ID: 

Find Unhandled Exceptions with Retrace

One of the great features of Retrace is it’s error monitoring capabilities. Retrace can automatically collect all .NET exceptions that are occurring within your application. This includes unhandled exceptions but can also include all thrown exceptions, or first chance exceptions.

Improve Your Code with Retrace APM

Stackify's APM tools are used by thousands of .NET, Java, PHP, Node.js, Python, & Ruby developers all over the world.
Explore Retrace's product features to learn more.

Learn More
situs toto pucuk4d indobet situs toto toto slot toto slot toto slot depo 10k 8kuda4d judi bola toto toto toto situs toto toto toto slot bobatoto daftar logototo situs toto situs toto toto bydplay patentoto situs toto slot 4D situs toto leon188 situs toto slot gacor terbaru toto toto situs toto ilmutoto mix parlay bayitoto mawar800 login popotogel prize hongkong malam ini toto joker11 toto slot lingkartoto ilmutoto https://foundvinylrecords.com/ depo 5k panen100 semibola MySlot188 monk4d popo togel https://asupantoto.co/ pompa4d slot pascol4d login pascol4d login sbobet88 sbobet88 https://nolimithoki.dev/ ini777 dinasti33 slot situs toto slot88 slot jepang slot depo 5k pucuk4d https://www.juara288.asia/ https://shopjoliboutique.com/ slot 4d RP888 toto toto pascol4d situs toto kari4d flokitoto slot gacor logototo slot depo 5k bobatoto bobatoto bobatoto sontogel PASCOL4D LOGIN mpo slot sumbartoto mataramtoto slot gacor situs toto slot situs slot situs toto pakde4d link slot sumbartoto Login bandar toto popotogel login slot gacor nobu99 situs toto logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo Slot Malaysia bobatoto istanaslot sumbartoto mataramtoto meteorbet88 login 33ag rp888 AMANAHTOTO www.pajaktoto.io kari4d kari4d kari4d kari4d pascol4d pascol4d pascol4d situs toto toto slot situs slot slot ilmutoto toto toto slot SLOT GACOR slot gacor slot bento11 link slot gacor AMANAHTOTO KARI4D PUCUK4D pascol4d situs slot situs slot toto slot titi4d ilmutoto situs toto KARI4D DVTOTO PUCUK4D AMANAHTOTO slot gacor toto toto slot slot AMANAHTOTO toto jinhoki AMANAHTOTO toto slot slot777 situs slot situs slot situs slot situs slot AMANAHTOTO
situs toto pucuk4d indobet situs toto toto slot toto slot toto slot depo 10k 8kuda4d judi bola toto toto toto situs toto toto toto slot bobatoto daftar logototo situs toto situs toto toto bydplay patentoto situs toto slot 4D situs toto leon188 situs toto slot gacor terbaru toto toto situs toto ilmutoto mix parlay bayitoto mawar800 login popotogel prize hongkong malam ini toto joker11 toto slot lingkartoto ilmutoto https://foundvinylrecords.com/ depo 5k panen100 semibola MySlot188 monk4d popo togel https://asupantoto.co/ pompa4d slot pascol4d login pascol4d login sbobet88 sbobet88 https://nolimithoki.dev/ ini777 dinasti33 slot situs toto slot88 slot jepang slot depo 5k pucuk4d https://www.juara288.asia/ https://shopjoliboutique.com/ slot 4d RP888 toto toto pascol4d situs toto kari4d flokitoto slot gacor logototo slot depo 5k bobatoto bobatoto bobatoto sontogel PASCOL4D LOGIN mpo slot sumbartoto mataramtoto slot gacor situs toto slot situs slot situs toto pakde4d link slot sumbartoto Login bandar toto popotogel login slot gacor nobu99 situs toto logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo logototo Slot Malaysia bobatoto istanaslot sumbartoto mataramtoto meteorbet88 login 33ag rp888 AMANAHTOTO www.pajaktoto.io kari4d kari4d kari4d kari4d pascol4d pascol4d pascol4d situs toto toto slot situs slot slot ilmutoto toto toto slot SLOT GACOR slot gacor slot bento11 link slot gacor AMANAHTOTO KARI4D PUCUK4D pascol4d situs slot situs slot toto slot titi4d ilmutoto situs toto KARI4D DVTOTO PUCUK4D AMANAHTOTO slot gacor toto toto slot slot AMANAHTOTO toto jinhoki AMANAHTOTO toto slot slot777 situs slot situs slot situs slot situs slot AMANAHTOTO