Why win32 exception are not caught by c# exception handling mechanism?

EDIT : as Pratik pointed out, the following answer applies to . NET 1.0 and . NET 1.1 only.

Starting with . NET 2.0, non-CLS exception should be caught as a RuntimeWrappedException Because Win32 exceptions do not derive from the . NET Exception class.

Try : try { } catch (Exception ex) { // . NET exception } catch { // native exception } See Catch non-CLSCompliant exceptions in general handlers for more information.

EDIT : as Pratik pointed out, the following answer applies to . NET 1.0 and . NET 1.1 only.

Starting with . NET 2.0, non-CLS exception should be caught as a RuntimeWrappedException. Because Win32 exceptions do not derive from the .

NET Exception class. Try : try { } catch (Exception ex) { // . NET exception } catch { // native exception } See Catch non-CLSCompliant exceptions in general handlers for more information.

4 This is not required in . Net 2.0 by default. All non CLS exception are rapped as a RuntimeWrappedException.

See msdn.microsoft. Com/en-us/library/ms404228. Aspx – Pratik Jul 30 '09 at 9:44 Win32Exception @Tommy Carlier : cf.

Pratik's link... – Mac Jul 30 '09 at 11:38.

While I don't know why your catch block doesn't work try using the Application ThreadException Event. This should catch any error in application threads. Add an event handler before calling Application.Run.

For your second answer definitely yes. I develop and maintain a enterprise winforms application that talk with a web service backend on background threads. If any webservice call crashes handle the application threadexception (and also appdomain unhandledexception) event, log and popup an error that business users can report and allow them to continue without crashing the application.

The execution of Application. Run is not throwing an error. That is happening on another thread (or at least asynchronously).

Its good idea to inform the user in friendly way that the application has failed before it disapears completely, however its not a good idea to just catch then continue.

Try subscribing to these events before your application starts (Application. Run) : AppDomain.CurrentDomain. UnhandledException.Application.ThreadException.

You could then get rid of your try catch block. I think it is bad practice to catch exceptions at the highest level, but you cannot avoid it! During development (Debug), those exceptions should not be caught and the application should do the nastiest thing possible (crash?).

In production (Release), you will want your application to degrade as nicely as possible even when unhandled exceptions occur. This is one of the few uses I found for the DEBUG preprocessor variable.

There you go : 2 answers in one. Which one are you going to for (or against)? – Mac Jul 30 '09 at 12:24.

You may need to catch as Win32Exception (or ExternalException) instead msdn.microsoft.com/en-us/library/system.... I seem to remember that Win32Exception inherits from ExternalException but ExternalException does not inherit from Exception so won't be caught by your code. Edit: See other answers for why this is wrong! Edit 2: As for the second part, as stated by AnthonyWJones It is good manners to let the user know that a problem has caused the application to close, however I would recommend using a plain English statement to the user, and logging the exception stack to a log file for your own use.

Both of these are Derived from Exception, hence on the surface of it the catch in the question should work. For why it isn't see my answer. – AnthonyWJones Jul 30 '09 at 9:23.

1) Win32 exceptions should be caught. Maybe the exception is being thrown from a background thread or the GC thread? 2) It depends on your app structure.

For example, if your error notification UI was tied to the main form somehow (e.g. You need to invoke the UI thread from a worker thread), then it would be silly to display a UI in a code block outside the code that runs the message loop. However, if your code example is single-threaded then it would be fine.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions