473,322 Members | 1,911 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Very strange exception handling problem

Hi,

I have a .NET solution that contains a dll project and a tester application
project which, of course, invokes the dll. The dll project has exception
handling in it. What's happening is that when I run the executable from
within the .NET studio environment, thrown exceptions are caught and handled
correctly (both debug and release mode). However, if I run the executable
from outside the .NET studio, none of the exceptions get caught. In fact,
what I get is a pop-up telling me an unhandled exception has occurred and
pointing me right back to where the (valid) exception was thrown from. I
thought perhaps it was something I was doing wrong in my solution so I
created a little bare-bones dll and test app but the same thing still happens.

Has anyone ever seen anything like this? Any ideas as to what the problem
might be would be greatly appreciated! Btw, I had another much more
experienced co-worker look at this as well and he couldn't figure it out
either.

Regards,
Robin
Jun 13 '06 #1
6 1771
Robin,
Can you post a "Short but Complete" code sample of exactly how you are
catching and handling an exception?
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Robin Riley" wrote:
Hi,

I have a .NET solution that contains a dll project and a tester application
project which, of course, invokes the dll. The dll project has exception
handling in it. What's happening is that when I run the executable from
within the .NET studio environment, thrown exceptions are caught and handled
correctly (both debug and release mode). However, if I run the executable
from outside the .NET studio, none of the exceptions get caught. In fact,
what I get is a pop-up telling me an unhandled exception has occurred and
pointing me right back to where the (valid) exception was thrown from. I
thought perhaps it was something I was doing wrong in my solution so I
created a little bare-bones dll and test app but the same thing still happens.

Has anyone ever seen anything like this? Any ideas as to what the problem
might be would be greatly appreciated! Btw, I had another much more
experienced co-worker look at this as well and he couldn't figure it out
either.

Regards,
Robin

Jun 13 '06 #2
Hi Peter,

Thanks for the quick response! Here's a simple example:

internal void doStuff()
{
try
{
doMoreStuff();
}
catch(System.Exception x)
{
MessageBox.Show(x.Message + x.Trace);
}
}

private void doMoreStuff()
{
// assume error condition occurs herehere
throw new Exception();
}

"Peter Bromberg [C# MVP]" wrote:
Robin,
Can you post a "Short but Complete" code sample of exactly how you are
catching and handling an exception?
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Robin Riley" wrote:
Hi,

I have a .NET solution that contains a dll project and a tester application
project which, of course, invokes the dll. The dll project has exception
handling in it. What's happening is that when I run the executable from
within the .NET studio environment, thrown exceptions are caught and handled
correctly (both debug and release mode). However, if I run the executable
from outside the .NET studio, none of the exceptions get caught. In fact,
what I get is a pop-up telling me an unhandled exception has occurred and
pointing me right back to where the (valid) exception was thrown from. I
thought perhaps it was something I was doing wrong in my solution so I
created a little bare-bones dll and test app but the same thing still happens.

Has anyone ever seen anything like this? Any ideas as to what the problem
might be would be greatly appreciated! Btw, I had another much more
experienced co-worker look at this as well and he couldn't figure it out
either.

Regards,
Robin

Jun 13 '06 #3
BTW, I did some further playing around and noticed that this failure to catch
exceptions only seems to occur when the exception is thrown from within a
windows form.

Regards,
Robin
Jun 13 '06 #4
I'm guessing from your description that this is a WinForms application.

When you run a WinForms application under control of the Visual Studio
debugger (regardless of whether it's compiled for release or debug
mode), execution flow is exactly the way the code reads: just what you
would expect.

However, when you run it directly under Windows, execution flow
changes. Someone explained the precise details to me once but my memory
is a bit foggy on details. I'll give the best explanation I can,
though.

If you invoke a dialog, something like this:

DialogResult result = myDialog.ShowDialog();

you would think that you could catch any exceptions raised by the
dialog like this:

try
{
DialogResult result = myDialog.ShowDialog();
...
}
catch (Exception e)
{
...
}

and any exception raised by the dialog would be caught in the "catch"
block. In Visual Studio, this is exactly what happens.

However, when you run directly under Windows, the system doesn't really
call your dialog and then just sit there inside the try block waiting
for it to return. Somehow (I'm not sure how), control returns to the
underlying windowing system, which shows the dialog. Control is often
returned to the underlying system at strategic points in your
application. That's why when that dialog throws an exception, the
exception bubbles up not to your calling code, but right back to the
windowing system and the CLR, where it is treated like an unhandled
exception.

As I said, my memory of this isn't perfect, and my example may not be
exactly right, but that's basically what's going on in WinForms.

The only way to catch these exceptions and log them is by handling the

AppDomain.UnhandledException

and

Application.ThreadException

events.

Robin Riley wrote:
Hi,

I have a .NET solution that contains a dll project and a tester application
project which, of course, invokes the dll. The dll project has exception
handling in it. What's happening is that when I run the executable from
within the .NET studio environment, thrown exceptions are caught and handled
correctly (both debug and release mode). However, if I run the executable
from outside the .NET studio, none of the exceptions get caught. In fact,
what I get is a pop-up telling me an unhandled exception has occurred and
pointing me right back to where the (valid) exception was thrown from. I
thought perhaps it was something I was doing wrong in my solution so I
created a little bare-bones dll and test app but the same thing still happens.

Has anyone ever seen anything like this? Any ideas as to what the problem
might be would be greatly appreciated! Btw, I had another much more
experienced co-worker look at this as well and he couldn't figure it out
either.

Regards,
Robin


Jun 13 '06 #5
Robin Riley wrote:
BTW, I did some further playing around and noticed that this failure to catch
exceptions only seems to occur when the exception is thrown from within a
windows form.

Regards,
Robin


Ahh yes, windows forms exceptions.

You need to attach an exception handler to the thread's ThreadException
event:

Expand|Select|Wrap|Line Numbers
  1. System.Windows.Forms.Application.ThreadException +=
  2. OnThreadException;
  3.  
And have a code handler:

Expand|Select|Wrap|Line Numbers
  1. /// <summary>
  2. /// Provides a last chance exception handler.
  3. /// </summary>
  4. public static void OnThreadException(object sender,
  5. System.Threading.ThreadExceptionEventArgs e)
  6. {
  7. log.Error("Unhandled Exception Occured", e.Exception);
  8. }
  9.  
Jun 13 '06 #6
Thanks to those who replied - I can now stop banging my head against the
wall!! :-)

Regards,
Robin
Jun 13 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Daniel Wilson | last post by:
I am having exception-handling and stability problems with .NET. I will have a block of managed code inside try...catch and will still get a generic ..NET exception box that will tell me which...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
2
by: Rhino | last post by:
I am trying to write a trigger/UDF combination that closely follows the example given in the following DeveloperWorks article:...
4
by: Ele | last post by:
When Exception handling disabled compiler still spits out "C++ exception handler used." Why is that? Why does it ask for "Specify /EHsc"? Thanks! c:\Program Files\Microsoft Visual Studio...
14
by: James Wong | last post by:
Hi! everybody, I'm facing a quite strange download problem. I use the following code to download an XML file to client side: With Response ' clear buffer Call .Clear() ' specify the...
2
by: Piedro | last post by:
Can someone reproduce the following error? I'm using the module at the bottom of my post to owner draw a menu items, I call the module from a form like this: Private Sub mnuOpen_DrawItem(ByVal...
11
by: chopsnsauce | last post by:
Here's the example: Dim frm As New FORM1 Try frm.show Catch ex As Exception msgbox ex.message
10
by: John Kraft | last post by:
Hello all, I'm experiencing some, imo, strange behavior with the StreamReader object I am using in the code below. Summary is that I am downloading a file from a website and saving it to disk...
13
by: Jen | last post by:
One user of my application is experiencing an exception "input string not in correct format". But it makes no sense where it is occurring. It is occurring when a string from a textbox ("172") is...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.