none
Will catch(Exception ex) catch a COMException? RRS feed

  • Question

  • I just read a post on stackoverflow that said if you have a catch-all catch statement it won't catch System.Runtime.InteropServices.COMException, something to do with the way interop does things. ie you must have a catch statement that specifies COMException, otherwise it won't be caught.

    "COMException may inherit from System.Exception, but it's still not caught in the COM interop with System.Exception. I've done a lot of interop with AutoCAD and this drove me crazy for quite awhile since I was so used to using the catch all instead of specifying my exact exceptions. – Noah "

    Anyone know if this is really the case, and what the details are? A brief search didn't find any discussion on this.

    - Rory
    Friday, May 22, 2009 6:34 PM

Answers

  • That cannot be accurate, I'm not buying.  COMException is specifically raised by the CLR's COM interop code as a result of a failure HRESULT return code from a COM method.  It is otherwise a completely normal exception and doesn't get treated specially by the exception filter.  COM code is not allowed to raise exceptions.  Nor would it know how to raise a managed exception.
    Hans Passant.
    Friday, May 22, 2009 7:20 PM
    Moderator

All replies

  • That cannot be accurate, I'm not buying.  COMException is specifically raised by the CLR's COM interop code as a result of a failure HRESULT return code from a COM method.  It is otherwise a completely normal exception and doesn't get treated specially by the exception filter.  COM code is not allowed to raise exceptions.  Nor would it know how to raise a managed exception.
    Hans Passant.
    Friday, May 22, 2009 7:20 PM
    Moderator
  • That's what I'm thinking too...

    This page describes how interop maps from HRESULTs to .net exceptions. There are a bunch of well-known HRESULTs that are mapped to specific Exception types, everything else results in a COMException. However, I know various crazy things do happen with interop so wouldn't be hugely surprised if something wacky like this did happen, but it would surely have to be regarded as a bug.

    - Rory

    Friday, May 22, 2009 7:39 PM
  • I think that a simple example should prove the original article wrong.

    -Karel
    Tuesday, May 26, 2009 7:12 PM
    Moderator