Link to home
Start Free TrialLog in
Avatar of tia_kamakshi
tia_kamakshiFlag for United Arab Emirates

asked on

Exception handling C#

Hi,

I am working on C#, ASP.net 2.0

In my global.aspx and on Application_Error,

I get the exception
Exception handledException = Server.GetLastError();
HttpContext.Current.Session.Add("LastError", handledException);

but at line
HttpContext.Current.Session.Add("LastError", handledException);

it says

An exception of type 'System.NullReferenceException' occurred in App_global.asax.lp6-olqc.dll but was not handled in user code


Additional information: Object reference not set to an instance of an object.

when the actual exception details are as below, says file does not exists

{System.Web.HttpException: The file '/Studio/VW/MainScreens/HomeP098098age.aspx' does not exist.
   at System.Web.UI.Util.CheckVirtualFileExists(VirtualPath virtualPath)
   at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile)
   at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile)
   at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean noAssert)
   at System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp, Boolean noAssert)
   at System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath)
   at System.Web.UI.PageHandlerFactory.System.Web.IHttpHandlerFactory2.GetHandler(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath)
   at System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig)
   at System.Web.HttpApplication.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)}
   


void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs
        HttpContext.Current.Response.ClearContent();
        Exception handledException = Server.GetLastError();
        HttpContext.Current.Session.Add("LastError", handledException);
       
        string applPath = Request.ApplicationPath;
        Server.Transfer(applPath + "/errorTemplate.aspx", true);
    }
   
   
   
But if some the error thrown by application by like Index outof bound then it works fine and my details are redirected to

Server.Transfer(applPath + "/errorTemplate.aspx", true);

but if file not found then it is not

Can anyone help me on this.

Thanks

Avatar of Göran Andersson
Göran Andersson
Flag of Sweden image

The code in the Application_Error method assumes that there is a Session object in the current context, but when the error is that the requested page doesn't exist, the Session object hasn't been created yet.

You have to check if the value of the HttpContext.Current.Session property is null or not, before you try to use it:
Exception handledException = Server.GetLastError();
HttpSessionState session = HttpContext.Current.Session;
if (session != null) {
   session.Add("LastError", handledException);
} else {
   // handle the error message differently.
}

Open in new window

Avatar of tia_kamakshi

ASKER

Yes, you are right the session is null, but why this is happening?

As user is logged in and we have the user information in session. When I am knowlingly giving wrong name to the page in URL

why in application_error method of global.asax says session is null.

But thereafter without doing anything If i go to the correct logged in page I can see loggedin user name

Please help me in this

Thanks
The check for the existance of the page is done before the cookies are examined to match the user with a session.

You can specify that a page should be session-less, so the page has to be parsed before the server even knows if sessions should be used or not.
Thanks for the details.

So what is the best way to transfer the details to my errorTemplate.aspx file, so that I can show user the error details in my aspx file.

If I create a new Session object in the else part, than I may loose my information stored in the session like logged in user information

if (session != null)
{
   HttpContext.Current.Session.Add("LastError", handledException);
}
else
{
}
 string applPath = Request.ApplicationPath;
 Server.Transfer(applPath + "/errorTemplate.aspx", true);
 
 
 Thanks
ASKER CERTIFIED SOLUTION
Avatar of Göran Andersson
Göran Andersson
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yeah. It helps

Many Thanks