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.
You can use Assert. ThrowsException<T> and Assert.
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.
The StackTrace property carries a stack trace that can be used to determine where the error occurs in the code.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With