If we have an action with the following code:
Exception exception = new Exception("0");
for (int count = 0; count < 10; count++)
{
exception = new Exception(count.ToString(), exception);
}
throw exception;
then only the inner exception (with message “0”) will get reported to the client:
{"Message":"An error has occurred.","ExceptionMessage":"0","ExceptionType":"System.Exception","StackTrace":null}
The reason is that we use Exception.GetBaseException() which automatically unwraps to the absolute inner exception.
A better model would be to an exception-unwrap implementation that only unwraps certain specific exceptions such as AggregateException and TargetInvocationException.
Henrik
Comments: Why does my beautiful outermost exception get thrown away? It tells the client what is wrong in terms that the client understands. My client does not care that a "FileNotFound" exception occured; it cares that the "InvalidPrescriptionState" exception occurred, for example. For example here is a web service method in my WEBAPI project that attempts to do some tricky math: [HttpGet] public JObject DoTrickyMath() { try { // try to do operation tricky math. it will fail due to unexpected programming bug int x = 5; int y = 1 / (5 - x); } catch (Exception ex) { // throw exception with more meaning to client; unfortunately the client will not see this message throw new InvalidOperationException("unable to do tricky math at this time; some kind of internal exception occured.", ex); } } In this example I want my client to see "InvalidOperationException" but unfortunately it sees DivideByZeroException. I want to include the InnerException because the exception will be logged by the exception filter registered in global.asax Application_Start(), and the stack trace in the log will be helpful. This seems so obvious to me: all error displays everywhere always display the outermost exception. I can't fathom when it makes sense to throw that stuff away. (Probably I am just showing my lack of experience or something, but there it is. I said it.) Thanks for the opportunity to provide feedback!!!
Exception exception = new Exception("0");
for (int count = 0; count < 10; count++)
{
exception = new Exception(count.ToString(), exception);
}
throw exception;
then only the inner exception (with message “0”) will get reported to the client:
{"Message":"An error has occurred.","ExceptionMessage":"0","ExceptionType":"System.Exception","StackTrace":null}
The reason is that we use Exception.GetBaseException() which automatically unwraps to the absolute inner exception.
A better model would be to an exception-unwrap implementation that only unwraps certain specific exceptions such as AggregateException and TargetInvocationException.
Henrik
Comments: Why does my beautiful outermost exception get thrown away? It tells the client what is wrong in terms that the client understands. My client does not care that a "FileNotFound" exception occured; it cares that the "InvalidPrescriptionState" exception occurred, for example. For example here is a web service method in my WEBAPI project that attempts to do some tricky math: [HttpGet] public JObject DoTrickyMath() { try { // try to do operation tricky math. it will fail due to unexpected programming bug int x = 5; int y = 1 / (5 - x); } catch (Exception ex) { // throw exception with more meaning to client; unfortunately the client will not see this message throw new InvalidOperationException("unable to do tricky math at this time; some kind of internal exception occured.", ex); } } In this example I want my client to see "InvalidOperationException" but unfortunately it sees DivideByZeroException. I want to include the InnerException because the exception will be logged by the exception filter registered in global.asax Application_Start(), and the stack trace in the log will be helpful. This seems so obvious to me: all error displays everywhere always display the outermost exception. I can't fathom when it makes sense to throw that stuff away. (Probably I am just showing my lack of experience or something, but there it is. I said it.) Thanks for the opportunity to provide feedback!!!