Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6176857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:10:31+00:00 2026-05-24T00:10:31+00:00

Code in Program.cs [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); try { Application.Run(new Form1());

  • 0

Code in Program.cs

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    try
    {
        Application.Run(new Form1());
    }
    catch (Exception ex)
    {
        MessageBox.Show("Blah...");
    }
}

In Form1 I have a button with the code throw new Exception("");.

If I run the application from Visual Studio, then my messagebox pops up (with message ‘Blah…’). But if I run the application from executable file, then the catch block doesn’t execute at all.

Why the difference?

I am using Visual Studio 2010, .NET 4.0, Windows XP.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:10:32+00:00Added an answer on May 24, 2026 at 12:10 am

    This is because the standard exception handling for a Windows Forms application behaves differently when the Visual Studio debugger is attached – normally the exception handler built into the Application.Run method catches unhandled exceptions so that it can do things like show the following dialog:

    Error dialog

    If it allowed the exception to be thrown outside of the Application.Run method then it would prevent the application from continuing if the user presses “continue” (as the catch is outside of the message pump).

    When debugging however this is disabled, presumably so that the debugger will jump straight into debugging mode on an unhandled exception rather than the above dialog being shown.

    If you wish to handle unhandled exceptions in your Windows Forms application then you should handle the Application.ThreadException Event. Alternatively you can alter this behaviour with the Application.SetUnhandledExceptionMode Method.

    You are by no means alone in being confused by this:

    • Is try/catch around whole C# program possible?
    • Exception handling problem in release mode (this question is misleadingly named – this isn’t related to the Release / Debug build setting)
    • C# Exceptions only caught when debugging?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: class Program { static void Main(string[] args) { new
Consider the following code: class Program { static void Main(string[] args) { Department deathStar
In a typical winform program.cs contains the entry point: static void Main(string[] args) {
When I run this code the program freezes,why? main() { int co; co=0; while
Assume that we have the following code: class Program { static volatile bool flag1;
I have the following code in my program: Thread getUsersist, getChatUsers; getUsersList = new
In C#, WinForms, VS2008, .NET 3.5... For this code: static class Program { ///
I try to follow http://edn.embarcadero.com/article/28604 for restarting Interbase. Here is some code: program IBRestart;
I have a winforms application.Winforms start with Program.cs where we have main() defined.I have
Here's some code (full program follows later in the question): template <typename T> T

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.