简体   繁体   English

如何绑定异常类和外部com异常的错误代码?

[英]How to bind exception class and error code of external com exception?

my .NET application calls method of object that located out of assembly boundaries. 我的.NET应用程序调用位于程序集边界之外的对象的方法。 When this method has thrown exception i've catched COMException. 当此方法引发异常时,我捕获了COMException。

Exception contains special code in ErrorCode field, for example -123. 异常在ErrorCode字段中包含特殊代码,例如-123。

try { // call outside object } catch(COMException e) { // e.ErrorCode == -123; 尝试{//调用外部对象} catch(COMException e){// e.ErrorCode == -123; } }

Is it posible to define exception class that will be thrown instead of COMException? 是否可以定义将抛出的异常类而不是COMException?

eg 例如

[ErrorCode of external error = -123] public class MyException : Exception; [外部错误的ErrorCode = -123]公共类MyException:异常;

and rewrite above code 并重写上面的代码

try { // call outside object } catch(MyException e) { //... } 尝试{//调用外部对象} catch(MyException e){// ...}

"Is it posible to define exception class that will be thrown instead of COMException?" “是否可以定义将抛出的异常类而不是COMException?”

No, but you could catch the COMException, wrap it in a custom exception class, and rethrow it. 不,但是您可以捕获COMException,将其包装在自定义异常类中,然后将其重新抛出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM