简体   繁体   English

如何检查类的属性是否抛出异常

[英]How to check if a property of a class has thrown an exception

How can I check if a getter/setter of a property in a class, has thrown an exception? 如何检查类中属性的getter / setter是否抛出异常? (Before accessing it) (在访问之前)

Example

For example in this picture, the ExitTime property of a process threw an exception of type System.InvalidOperationException 例如,在此图片中,进程的ExitTime属性引发了System.InvalidOperationException类型的异常

在此输入图像描述

If I use process.ExitTime , I get another runtime exception: No process is associated with this object. 如果我使用process.ExitTime ,我会得到另一个运行时异常: No process is associated with this object.

Technically it hasn't thrown an exception before you use the accessor method. 从技术上讲,它在使用访问器方法之前没有抛出异常。 The only reason that window shows it is because it tried to get the property . 窗口显示它的唯一原因是它试图获取属性

So you can't detect this. 所以你无法检测到这一点。 All you can do is wrap the accessing code in a try/catch block to catch the exception when it is thrown. 您所能做的就是将访问代码包装在try/catch块中,以便在抛出异常时捕获异常。

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

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