• 0

[C#] Catch Application exception


Question

			try
			{
				Application.Run(new Form1());
			}
			catch (Exception e) 
			{
				 //report exception to server

Through visual studio, the above catch block is actually invoked. (I'm using it for error reporting back to my site, to fix problems and such).

But when I run the standalone exe, the exception is thrown into .NET's exception handling mechanism, and thus is never caught by my try catch block. It mentions JIT (just in time debugging), but I don't know if it has to do with it.

Is this the right way I should be doing this? It makes sense and works through Visual Studio, but not as standalone (which I need).

Link to comment
https://www.neowin.net/forum/topic/619252-c-catch-application-exception/
Share on other sites

7 answers to this question

Recommended Posts

  • 0

I'm throwing the exception on my own...

The point is that in visual studio it propogates up the calling stack to the first Application.Run... but when run as a stand alone, .NET steps in for the error catching... which I don't want it to do. I want my catch at the top to handle it all..

That make sense?

  • 0

What you're after is global exception handling, and there's an event you need to subscribe to in order to catch this and not let the .NET framework spit out the dialog, I forgot the event name but it's under Application object, something like Application.ThreadException

Look for an event under that object, and subscribe to it.

  • 0
What you're after is global exception handling, and there's an event you need to subscribe to in order to catch this and not let the .NET framework spit out the dialog, I forgot the event name but it's under Application object, something like Application.ThreadException

Look for an event under that object, and subscribe to it.

You can read this for more information. See below for a quote which may point you in the right direction.

By default, unhandled exceptions in a Windows Forms application will display the Unhandled Exception Dialog. You can get the behavior you want by either hooking the Application.ThreadException event (rather than the AppDomain.UnhandledException event) or by setting Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException) in your Main() method.
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Cooler Master MasterFrame 600 PC case is now 33% off on Amazon by Ivan Jenic The Cooler Master MasterFrame 600 is currently $109.99 on Amazon, down from its original $164.99 list price. That's 33% off and $55 saved on this premium aluminum mid-tower case with a modular design. If you're upgrading your PC case and want something that doesn't force you into a rigid layout, the MasterFrame 600 is worth a look. The case is built around the Cooler Master's FreeForm 2.0 platform, which lets you reconfigure the internal structure according to your hardware. Magnetic side panels allow for straightforward adjustments, and the case supports everything from Mini-ITX to E-ATX motherboards without compromise. There's also generous cooling headroom. Four pre-installed PWM fans handle airflow out of the box. GPU clearance goes up to 410mm, and the case supports radiators up to 420mm with room for three simultaneously. Truth be told, this might not be the prettiest case on the market, but it’s highly functional. The aluminum construction keeps the whole thing lightweight despite its size, and the finish looks noticeably better than the plastic mid-towers competing at this price point. If you want a serious, flexible case that prioritizes function over flashy aesthetics like RGB lighting, the MasterFrame 600 delivers at a reasonable price. Cooler Master MasterFrame 600 - $109.99 | 33% off on Amazon This Amazon deal is US-specific and not available in other regions unless specified. This is a first-party seller link (at the time of article publishing); ensure that you also purchase from a first-party seller link only. If you don't like it or want to look at more options, check out the previous deals that we have covered, OR you can also visit Amazon US deals page. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
    • DK, I don't use the extended channel, I'm always on the latest release.
    • Your daily driver? Weird, seeing that you don't seem to understand how your own browser downloads and installs its updates. What you posted earlier is factually incorrect...
  • Recent Achievements

    • Rookie
      restore went up a rank
      Rookie
    • Very Popular
      AndrewSteel earned a badge
      Very Popular
    • Veteran
      Taliseian went up a rank
      Veteran
    • One Month Later
      Clizby earned a badge
      One Month Later
    • One Month Later
      Timaximus earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      509
    2. 2
      +Edouard
      162
    3. 3
      PsYcHoKiLLa
      155
    4. 4
      ATLien_0
      82
    5. 5
      Steven P.
      80
  • Tell a friend

    Love Neowin? Tell a friend!