I just ran into an issue where someone was passing ex.InnerException
to a method, where ex
was the root. Since the parameter was also called ex
it led to some confusion in the debugger when I looked at the originally caught exception. This was likely the result of some careless refactoring.
e.g.:
public void MyMethod(string input)
{
try {
Process(input);
} catch (Exception ex) { // <- (2) Attempting to view ex here would show null
_logger.log(ex);
LogInner(ex.InnerException);
}
}
private void LogInner(Exception ex)
{
_logger.log(ex); // <- (1) NullReferenceExeption thrown here
if(ex.InnerException != null)
LogInner(ex.InnerException);
}
This was refactored as such:
public void MyMethod(string input)
{
try {
Process(input);
} catch (Exception ex) {
LogExceptionTree(ex);
}
}
private void LogExceptionTree(Exception exception)
{
_logger.log(exception);
if(exception.InnerException != null)
LogExceptionTree(exception.InnerException);
}