Solved

Exception handling C#

Posted on 2007-11-23
6
1,812 Views
Last Modified: 2008-02-01
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

0
Comment
Question by:tia_kamakshi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20340540
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

0
 

Author Comment

by:tia_kamakshi
ID: 20349733
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
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20350164
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tia_kamakshi
ID: 20350503
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
0
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 500 total points
ID: 20351276
I'm not sure, but the context should survive the transfer, so that you could store the information in the context object:

HttpContext.Current.Items["LastError"] = handledException;
0
 

Author Comment

by:tia_kamakshi
ID: 20356623
Yeah. It helps

Many Thanks
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question