2

I have some piece of code called Finally() which handles unhandled exceptions by subscribing to UnhandledException event (I'm talking about WinForms).

AppDomain.CurrentDomain.UnhandledException += (_, __) => Finally(__.ExceptionObject as Exception);

Where should I put this line? Into the static Program() constructor or into the static void Main() method? Is there any differences?

  • you can subscribe anywhere in the `Main` method but before an `unhandled exception` has occured. – Amit Kumar Ghosh Jul 25 '15 at 05:54
  • Are you looking for this [UnhandledException](http://stackoverflow.com/a/8148174/1257607)? – DanielV Jul 25 '15 at 09:17
  • Also, this can do the trick for you: [Application.SetUnhandledExceptionMode Method](https://msdn.microsoft.com/en-us/library/system.windows.forms.application.setunhandledexceptionmode(v=vs.110).aspx) – DanielV Jul 25 '15 at 09:19

1 Answers1

0

Although they both are called in the beginning of your program, I would place it in the Main() method because any exceptions that occur from within the static constructor are harder to debug.

C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72