简体   繁体   中英

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. When this method has thrown exception i've catched COMException.

Exception contains special code in ErrorCode field, for example -123.

try { // call outside object } catch(COMException e) { // e.ErrorCode == -123; }

Is it posible to define exception class that will be thrown instead of COMException?

eg

[ErrorCode of external error = -123] public class MyException : Exception;

and rewrite above code

try { // call outside object } catch(MyException e) { //... }

"Is it posible to define exception class that will be thrown instead of COMException?"

No, but you could catch the COMException, wrap it in a custom exception class, and rethrow it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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