locked
Exception handling on session expiration RRS feed

  • Question

  • User1057158394 posted

    Hello,

    I have a webforms application and it uses ELMAH for all error reporting. The application throws an error "Object reference not set to an instance of an object." when it tries to access the object stored in session after the session expires. I want to distinguish the real exceptions that are caused by code issue rather than the one that is thrown due to session expiry.

    I would like to know how to handle this in ASP.NET. Is to possible to distinguish if the exception thrown is due to an expired session?

    Please assist.

    Thanks
    Dan

    Friday, January 8, 2016 4:37 PM

Answers

  • User475983607 posted

    I have a webforms application and it uses ELMAH for all error reporting. The application throws an error "Object reference not set to an instance of an object." when it tries to access the object stored in session after the session expires. I want to distinguish the real exceptions that are caused by code issue rather than the one that is thrown due to session expiry.

    Sounds like a real exception to me.  The user requested a resource but the transaction failed due to an expired session.

    I would like to know how to handle this in ASP.NET. Is to possible to distinguish if the exception thrown is due to an expired session?

    It's common practice to check if session is null before trying to retrieve a value.  I would go through the code and fix it rather than than ignore the error.  Ignoring errors has s tendency to bit you later.

    I'm not familiar with ELMAH but I am familiar with application wide error handling.  If I wanted to ignore error due to an expired session, I would simply check if Session is empty in the error handler.  If Session is empty then bypass logging.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 9, 2016 7:06 PM
  • User-2057865890 posted

    Hi AaravorAaradhana,

    Before using Session, we should check if it exists.

    if (Session["SessionName"] == null)
    {
        Response.Redirect("YourPage.aspx");
    }
    else
    {
        // Do whatever you were going to do.Use ELMAH for error
    }

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 14, 2016 9:48 AM

All replies

  • User475983607 posted

    I have a webforms application and it uses ELMAH for all error reporting. The application throws an error "Object reference not set to an instance of an object." when it tries to access the object stored in session after the session expires. I want to distinguish the real exceptions that are caused by code issue rather than the one that is thrown due to session expiry.

    Sounds like a real exception to me.  The user requested a resource but the transaction failed due to an expired session.

    I would like to know how to handle this in ASP.NET. Is to possible to distinguish if the exception thrown is due to an expired session?

    It's common practice to check if session is null before trying to retrieve a value.  I would go through the code and fix it rather than than ignore the error.  Ignoring errors has s tendency to bit you later.

    I'm not familiar with ELMAH but I am familiar with application wide error handling.  If I wanted to ignore error due to an expired session, I would simply check if Session is empty in the error handler.  If Session is empty then bypass logging.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 9, 2016 7:06 PM
  • User753101303 posted

    Hi,

    It is quite common to use Session as a per user, browser session lifetime "cache". ie rather than accessing session variables directly you access them through a property so that the session value can be restored as needed in a new session, avoiding those exceptions.

    You can also use Session_Start or Sessio.IsNewSession. Your use case could help but IMO you should be able to prevent most if not all exceptions caused by a non initialized session variable.

    Monday, January 11, 2016 10:29 AM
  • User-2057865890 posted

    Hi AaravorAaradhana,

    Before using Session, we should check if it exists.

    if (Session["SessionName"] == null)
    {
        Response.Redirect("YourPage.aspx");
    }
    else
    {
        // Do whatever you were going to do.Use ELMAH for error
    }

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 14, 2016 9:48 AM