$exception is defined in “locals”

不想你离开。 提交于 2019-12-10 13:04:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!