抛出COMException但未在C#中捕获

时间:2015-10-26 12:45:59

标签: c# comexception

我有以下代码在按钮点击时触发:

        private void buttonConnect_Click(object sender, EventArgs e)
    {
        _BMDSwitcherConnectToFailure failReason = 0;
        string address = textBoxIP.Text;

        try
        {
            // Note that ConnectTo() can take several seconds to return, both for success or failure,
            // depending upon hostname resolution and network response times, so it may be best to
            // do this in a separate thread to prevent the main GUI thread blocking.
            m_switcherDiscovery.ConnectTo(address, out m_switcher, out failReason);
        }
        catch (COMException)
        {
            // An exception will be thrown if ConnectTo fails. For more information, see failReason.
            switch (failReason)
            {
                case _BMDSwitcherConnectToFailure.bmdSwitcherConnectToFailureNoResponse:
                    MessageBox.Show("No response from Switcher. Are you sure the IP is correct?", "Error");
                    break;
                case _BMDSwitcherConnectToFailure.bmdSwitcherConnectToFailureIncompatibleFirmware:
                    MessageBox.Show("Switcher has incompatible firmware", "Error");
                    break;
                default:
                    MessageBox.Show("Connection failed for unknown reason", "Error");
                    break;
            }
            return;
        }

        SwitcherConnected();
    }

我注意到在不同的机器上,程序无法运行。我设置VS以打破特定错误,我明白了:

Exception thrown: 'System.Runtime.InteropServices.COMException'

Additional information: Error HRESULT E_FAIL has been returned from a call to a COM component.

它将在以下行中抛出:

_switcherDiscovery.ConnectTo(address, out m_switcher, out failReason);

我正在努力弄清楚代码没有抓住这个异常的内容。

1 个答案:

答案 0 :(得分:-1)

检入您选择此异常类型的Visual Studio调试选项:

调试 - >例外

展开CLRE(公共语言运行时异常)

然后展开 - System.Runtime.InteropServices