4

My program is generating a System.ComponentModel.Win32Exception within a try/catch block and the exception is not being caught. The code is very simple:

try { lAFE.MinimumSize = sz1; // lAFE=Label, sz1 = Size } catch (Exception ex) { MessageBox.Show("afe: " + ex.Message); } 

The program runs through this code block hundreds of times without a problem then suddenly it generates this exception and it is not caught.

What can cause an exception like this not to be caught.

This application uses a lot of memory and the exception always occurs when the memory usage reaches about 305KB.

Any advice would be greatly appreciated.

6
  • Whats the exception? Commented Jan 29, 2015 at 23:05
  • 1
    Remove the Exception ex part. Only do catch { your stuff } for native exceptions. Commented Jan 29, 2015 at 23:06
  • 3
    possible duplicate of Why win32 exception are not caught by c# exception handling mechanism Commented Jan 29, 2015 at 23:06
  • out of curiosity - did you try to catch the specific exception, in this case the Win32Exception ? if possible, try to stay away from just an empty catch block as much as possible as it is bad practice let the war begin. Bad due to performance and poor coding design and not then being able to catch specific exceptions and acting upon it. Commented Jan 29, 2015 at 23:09
  • The performance doesn't change just because he has an empty catch block. The performance is not even effected until an exception is actually thrown. Commented Jan 30, 2015 at 0:24

2 Answers 2

5

Because Win32 exceptions do not derive from the .NET Exception class. Try :

try { } catch (Exception ex) { // .NET exception } catch { // native exception } 

You can read this article:

A catch block that handles Exception catches all Common Language Specification (CLS) compliant exceptions. However, it does not catch non-CLS compliant exceptions. Non-CLS compliant exceptions can be thrown from native code or from managed code that was generated by the Microsoft intermediate language (MSIL) Assembler. Notice that the C# and Visual Basic compilers do not allow non-CLS compliant exceptions to be thrown and Visual Basic does not catch non-CLS compliant exceptions. If the intent of the catch block is to handle all exceptions, use the following general catch block syntax.

C#: catch {}

Sign up to request clarification or add additional context in comments.

4 Comments

I am not sure if this will solve the problem, the op explicitly states he is getting a System.ComponentModel.Win32Exception
When I tried what you suggested above the compiler complained about the second catch stating that the preceding catch already catches ALL exceptions.
There is no way this is the answer.
What is indeed wrong: System.ComponentModel.Win32Exception does inherit Exception. If the former is "CLS compliant", I do not know...
0

Is this code run in another thread/task/async method?

Here is an example of some types of exceptions that "can't" be caught.

UnmanagedFunctionPointer causes stackoverflow when using .NET 4.0, 3.5 works

In this case a stack overflow exception crashes the exception handling, so whilst it could be caught in some circumstances, in the above it is not.

Can you post some more context of how this method is being called? I will update this answer.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.