473,734 Members | 2,211 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1795
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.Ex ception 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.ShowDi alog();

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

try
{
DialogResult result = myDialog.ShowDi alog();
...
}
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.Unhan dledException

and

Application.Thr eadException

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
2341
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 assemblies are loaded before shutting down. In one case, some of my DB-accessing code didn't handle a NULL value properly. But try...catch wouldn't catch the exception and keep going. I'd just get the error message and then it would shut the...
7
6005
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
2
3687
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: http://www-106.ibm.com/developerworks/db2/library/techarticle/0205bhogal/0205bhogal.html Unfortunately, when my UDF starts to compose the email, something goes wrong and DB2 begins writing to db2diag.log - and writing, and writing, and writing. Millions of lines get written if I don't kill the processes. I've...
4
5134
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 .NET 2003\Vc7\include\xstring(1453) : warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
14
1887
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 type of the downloadable file
2
2647
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 sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles mnuOpen.DrawItem Dim Ic As New Icon(Application.StartupPath & "\101_72.ico") DrawItems(e, mnuOpen, Ic) End Sub
11
5599
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
2337
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 for further parsing. I know, I could use the WebClient and it would be easier, but I don't have the flexibility I want with it. This code appears to work exactly the way I want unless the user cancels the the background operation. In that...
13
10392
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 being convert to an Int16 (using Convert.ToInt16). How can that be? There are other text boxes that are used in the identical fashion and they don't generate the exception. All there are many other machines running my application that don't...
0
8776
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9236
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9182
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6735
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.