简体   繁体   中英

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? (Before accessing it)

Example

For example in this picture, the ExitTime property of a process threw an exception of type System.InvalidOperationException

在此输入图像描述

If I use process.ExitTime , I get another runtime exception: 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.

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