I have a loop that runs multiple times and is prone to exceptions (it is web based). However, I don’t want to throw anything directly from the method as I want it to move on the next iteration in case of an error. So what I did was I added a reference parameter that the caller passes, and all exceptions are put there.
I want to return to the user the Exception as well as the fault URL, so I created the following object:
class KeywordException
{
Exception ex;
string faultURL;
}
It is returned to the user as ref List<KeywordException> exceptions
Ok it’s all good so far, and I catch exceptions like so:
catch (ArgumentNullException ane)
{
exceptions.Add(new KeywordException(ane, URL));
continue;
}
It works and the exception is returned to the user, but how can I from the calling code know what the exception actually was? All I get is Exception when it is actually an ArgumentNullException in this case.
The problem is that your KeywordException class is storing the exception as an Exception (which makes sense and is probably correct).
Why do you want to know the exact type of exception? Is it to display a message to the user? In this case you can use reflection to get the concrete type of the ex variable: