问题
H
We have log utility function that we call pretty often and I wanted to spice it up by adding a call to Server.GetLastError()
and if there is an error to log as well.
The log function is part of separate project so I tried to use HttpContext.Current.Server.GetLastError()
(As I do for the Request, ServerVariables and Session properties).
During the testing I created simple exception:
int i=0, j=0;
try
{
int k = i / j;
}
catch (Exception E)
{
Tools.CooLog("in");
}
Tools.CooLog("out");
In order to find out if "HttpContext.Current.Server.GetLastError()" will return the exception when Tools.CooLog("out");
is called.
Instead I had two big surprises
1. In both of the calls HttpContext.Current.Server.GetLastError()
returned null.
2. And maybe the weirdest is that during the first call of CooLog in the locals section I saw a bit of my young, handsome and PHP version - I saw that there is value called $exception and surprisingly it ha the exception that HttpContext.Current.Server.GetLastError()
failed to retrieve!

So my questions are
1. Why the HttpContext.Current.Server.GetLastError() returns null ? (HttpContext.Current.Request.ServerVariables
works fine)
2. Where that $exception comes from? is there a way to use it? (in the second call to CooLog the variable is undefined)
回答1:
That is a special debugger variable. You cannot access it via code.
The reason you don't see it in the Watch window, is that the exception has either been:
- Handled
or
- The Application.Error event has not been fired yet.
回答2:
$exception
is just the exception that was the reason the debugger paused execution of your program. This is the same as the exception you can access in your catch block, in your case E
.
来源:https://stackoverflow.com/questions/7268947/exception-is-defined-in-locals