7

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

enter image description here

If I use process.ExitTime, I get another runtime exception: No process is associated with this object.

1
  • 1
    Good question. I've had Properties that have caused exceptions, especially if a Property that is also a Factory (Get Property creates an instance if it doesn't already exist). In that case, I never get an error, and it can be hard to track down. Commented Sep 18, 2015 at 18:56

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.