Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

enter image description here

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

like image 617
A-Sharabiani Avatar asked Sep 18 '15 18:09

A-Sharabiani


People also ask

Which assert method is used to validate that a method throws a particular exception or not?

You can use Assert. ThrowsException<T> and Assert.

How do you test a method using exceptions?

Test for Exceptions using xUnit Throws<T> with an exception type, and an Action which is supposed to throw an exception. In our case, If num2 is zero then, capture the "ArithmeticException". We are going to test the below cases, num1 is non-zero and num2 is zero.

Which of the following properties of exception class is used to find the code that causes exception?

The StackTrace property carries a stack trace that can be used to determine where the error occurs in the code.


1 Answers

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.

like image 58
BradleyDotNET Avatar answered Oct 24 '22 02:10

BradleyDotNET