473,487 Members | 2,622 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

App works in VS's Debug mode, but does't work in run mode!

79 New Member
Hi.
This one is really strange and annoying.
The main method of a Windows Application:

static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);

try
{
Application.Run(new MainForm());
}
catch (Exception ex)
{
MessageBox.Show("Caught exception in Main method.")
}
}

I've put the Run method inside a try-catch block.

When an exception is caught inside a method that belongs to the MainForm class, I throw that exception, expecting to see the MessageBox called from the Main method.

Things happen as expected when I run the program in debug mode form VS.

However, if I run the exe file directly from the Debug folder of my project, the program behaves differently, in the sense that the message box does not pop up anymore, and instead the much-hated "Unhandled exception has occured in your application." window shows.

The same happens if I generate a beep on the PC speaker(System.Console.Beep()) instead of showing the message box.

I really don't understand. Why is the exception caught only in Debug mode??

Thanks.
May 25 '07 #1
5 1823
kenobewan
4,871 Recognized Expert Specialist
Its happened because your code did not get a chance to run before an error occurred. Unfortunately, you stuck with old fashioned debugging parsing the code until it works and then building it back up again. BTW - I believe that the VS debugger is better at vb.net rather than C#.
May 25 '07 #2
Plater
7,872 Recognized Expert Expert
Are you purposely triggering an exception to test out the try catch block?
Because if not, one solution would be to just FIX the error so it doesn't happen.

Also that's not a place that's garunteed to catch every exception. Threads are invoked internally for window creation and if an exception happens on another thread, it doesn't always make it's way back to the calling thread. Leaving the exception uncaught
May 25 '07 #3
blackjack2150
79 New Member
Thank you. Someone suggested a shady workaround, but it seems to do the trick. Here it it:

Add

Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Appli cation_ThreadException);

before calling Application.Run(... and the corresponding event handler like this:

static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
throw e.Exception;
}


Now every exception will be caught by your "top level" try/catch.
May 28 '07 #4
blackjack2150
79 New Member
Are you purposely triggering an exception to test out the try catch block?
Because if not, one solution would be to just FIX the error so it doesn't happen.
I can't be certain that no error will occurs. The application connects to a database and a exception could occur if the user misspells his username at login.

Damn, I hate to see things behaving differently than what logic and common sense tells they should behave.
May 28 '07 #5
Plater
7,872 Recognized Expert Expert
You should have smaller try/catch blocks surrounding those kinds of things
May 29 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
1732
by: Ariel | last post by:
I have an application that works correctly in debug mode but it does not work in release. I am not using anything of the class system.diagnostics. I tried to disable the optimization code, but...
8
5448
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
2
4059
by: PJ | last post by:
I'm at the end of my rope, I've tried everything I can think of and, while the problem is all over google, no answer has worked for me. I'm getting the following error while trying to start my web...
7
2880
by: Srinivasa Rao | last post by:
I have read in one article that when we compile the application in release mode, all the debug classes and properties will be automatically removed from the code. I tried to implement this thing by...
10
6997
by: Bob | last post by:
This a bit of a second post on same subject, my apologies. IsinRole function only works with built-in roles. If I create a new group on my domain controller then add an existing user to that...
3
3494
by: jt | last post by:
For some stupid reason, I can't get this to work in Release mode, but works well in Debug mode. Below is the function: Here is the line: pos=strpos(pszCmdLine,cmdLineStr); // in release mode...
7
3036
by: =?Utf-8?B?R3JpZ3M=?= | last post by:
Hello, After getting some posts on forums.microsoft.com but no solution I was asked to post over here. Hopefully someone here can help with my problem. I have a Windows Forms application...
3
15496
by: Bob Johnson | last post by:
It is my understanding - and please correct me if I'm wrong - that when building a project in debug mode, I can deploy the .pdb file along with the ..exe and thereby have access to the specific...
3
3721
by: kimiraikkonen | last post by:
Hi, I was looking for an example on CodeProject and saw an interesting thing, i downloaded the article source code and converted to my VB 2005 Express and compiled with no problem, however when i...
0
7106
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7181
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4874
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
600
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
267
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.