How do I catch an exception in a GUI thread?
An exception is thrown in a user control based on a picture box, causing it to show the typical error image (red X). Since the GUI thread caught and handled the exception itself, I cannot easily find out where the exception occurred and debug.
I'm currently wrapping the whole OnPaint code in a开发者_运维技巧 try-catch, and was able to debug the code, but I found it quite tedious so I wondered if there is a way to break into debugger from a GUI thread exception.
It already works this way by default. The UI thread exception handling method is controlled by Application.SetUnhandledExceptionMode(). The default is UnhandledExceptionMode.CatchException so that the ThreadException event is raised and, by default, creates a ThreadExceptionDialog.
However, if a debugger is attached then it overrides this mode. So that an exception will always be unhandled if there is no active catch clause. So that the debugger will stop, allowing you to diagnose the problem. By writing your own try/catch, you prevent this from working.
Do beware that OnPaint() can be special, particularly for PictureBox. It has a try/catch clause, catching an unhandled exception and painting a red cross. This is a bit unusual but necessary because it supports the ImageLocation property. Which lets it display images from a potentially unreliable network source. The best way to trouble-shoot exceptions in that case is with Debug + Exceptions, tick the Thrown checkbox. This forces the debugger to always stop on an exception, even if it isn't unhandled.
精彩评论