Reputation: 23830
I have an ASP.net website with C#. Current framework is 4.5
I have a global unexpected error catching function.
It is located at global.asax
Here how i catch
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
if (Server.GetLastError() != null)
if (Server.GetLastError().GetBaseException() != null)
{
Exception objErr = Server.GetLastError().GetBaseException();
ErrorLogger.LogError(Request.Url.ToString(), objErr.Message.ToString(), objErr.StackTrace.ToString());
if (objErr.Message.IndexOf("does not exist") != -1)
{
Response.RedirectPermanent("Error404.aspx");
}
}
}
Now it catches errors and many times there are plenty of information to debug the error. However there is 1 error that i can not get enough information.
It is Object reference not set to an instance of an object.
It doesn't give any information about which object it is. Are there any way to get more information about it ?
Here a typical error
Object reference not set to an instance of an object.
at gamepage.Page_Load(Object sender, EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
Thank you very much for answers.
Upvotes: 1
Views: 1644
Reputation: 1620
One thing you could do to help with tracking down the problem would be to find the method that has errored, and possibly even the line of code within that by performing a runtime stacktrace, something like:
var st = new StackTrace();
var errorInfo = String.Join("...", st.GetFrames().Select(x =>
{
var m = x.GetMethod();
var t = m.DeclaringType;
return String.Format("{0}.{1} @ {2}:{3}:{4}", t == null ? "" : t.Name, m.Name, x.GetFileName(), x.GetFileLineNumber(), x.GetFileColumnNumber());
});
that will allow you to find the code and fix it. I've not used such an approach in a global error trap such as the Application_Error event handler you are using, but I have used this approach to see where calls are coming from in other scenarios successfully, often to track down event handlers that are subscribed to an event.
Upvotes: 1
Reputation: 481
Use try..catch when you use any object and throw error with inner exception and stacks trace so you can log that error and get more information about specific error.
Upvotes: 0
Reputation: 218847
A NullReferenceException
does not carry information about the variable which is null
, no.
It doesn't give any information about which object it is.
That's just it... it isn't an object. It's a reference to where an object is expected, but there's no object there. null
is literally the absence of information.
The way to handle a NullReferenceException
is to carefully craft your code so that either:
NullReferenceException
with simply a custom message) or in some way log/persist the information and meaningfully continue execution of the application.Since no code or developer is perfect, it's fully expected that from time to time one will slip through the checks and balances. This is ok. It should be easy to identify which object is null
in this case. The stack trace should point to a specific method (perhaps even a specific line of code, depending on what information is available to the runtime) and there shouldn't be a whole lot of potential NullReferenceExceptions
there. If there are, the method is sloppy and should be cleaned up anyway.
When it happens, identify the object which was null
by debugging and investigation, and update the code to handle the potential null
case for that object.
Upvotes: 3