Webアプリケーションで例外を処理するための次のコードがあります。
application.Response.Clear();
application.Response.Status = Constants.HttpServerError;
application.Response.TrySkipIisCustomErrors = true;
LogApplicationException(application.Response, exception);
try
{
WriteViewResponse(exception);
}
// now we're in trouble. lets be as graceful as possible.
catch (Exception exceptionWritingView)
{
// Exception wrapper = ??
WriteGracefulResponse(exceptionWritingView);
}
finally
{
application.Server.ClearError();
}
ここでの問題は、応答をとしてレンダリングしようとしているときに例外が発生した場合、元の例外を(とにかく)ViewResult
「抑制」し、エラーがスローされた原因を表示するだけであるということです。View
ViewResult
exception
両方を取り、両方でexceptionWritingView
1つの例外を作成exceptionWritingView
し、上部に配置します。
これは:
exceptionWritingView
-> inner exceptions of exceptionWritingView
-> original exception that raised the unhandled exception handler
-> its inner exceptions
しかし、オブジェクトにInnerException
プロパティを設定することはできません。exception
では、どうすればこれを達成できますか?
「最高の」状態で、Exception
を使用して新しいものを作成することはできますが、プラスnew Exception(exceptionWritingView.Message, exception)
の一部を失うことになり、持っていた可能性のあるものを失うことになります。StackTrace
InnerException
exceptionWritingView
ここでの唯一の方法は反射ですか?振り返ってそれをするのはそれほど恐ろしいことでしょうか?