At this point, you get two different types of errors.
- Errors thrown by the compiler.
- Errors that your program throws.
The compiler can throw any error there , for example, it cannot find a literal control in the code. In this case, your page does not even exist, and there is no way to get it at that moment and give this JavaScript warning.
Errors in the runtime of your program , such as a null exception, can also stop the page displaying, and this also stops the page, and you cannot have a page handler.
So, HttpContext.Current.Handler is NOT a page .
In general, Application_Error is used to capture and log unhandled errors , to resolve them later and possibly show the user a better error page.
If you try to see your error while debugging, you can use the code:
void Application_Error(object sender, EventArgs e) { Exception LastOneError = Server.GetLastError(); if (LastOneError != null) { Debug.Fail("Unhandled error: " + LastOneError.ToString()); LogTheError(LastOneError); } }
User Error Page
The only way I can do something like this is to redirect when an error appears on a new page explaining the error, but the custom error page already does that.
More details
This is how I handle my user errors and stay on the same page. Checking the file is to avoid a possible closed loop that could lead to a pool failure.
string cTheFile = HttpContext.Current.Request.Path; if (!cTheFile.EndsWith("ErrorHandlePage.aspx")) Server.Transfer("~/ErrorHandlePage.aspx");