Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$exception is defined in "locals"

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!

$exception is defined and HttpContext.Current.Server.GetLastError() fails

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)

like image 862
SimSimY Avatar asked Feb 03 '23 14:02

SimSimY


1 Answers

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.
like image 133
leppie Avatar answered Feb 05 '23 02:02

leppie