views:

152

answers:

2

I am getting an IndexOutOfRange exception on the following line of code:

var searchLastCriteria = (SearchCriteria)Session.GetSafely(WebConstants.KeyNames.SEARCH_LAST_CRITERIA);

I'll explain the above here:

  1. SearchCriteria is an Enum with only two values
  2. Session is the HttpSessionState
  3. GetSafely is an extension method that looks like this:

    public static object GetSafely(this HttpSessionState source, string key)
    {
      try { return source[key]; }
      catch (Exception exc) { log.Info(exc); return null; }
    }
    
  4. WebConstants.KeyNames.SEARCH_LAST_CRITERIA is simply a constant

I've tried everything to replicate this error, but I cannot reproduce it. I am beginning to think the stack trace is wrong. I thought perhaps the exception was actually coming from the GetSafely call, but it is swallowing the exceptions, so that can't be the case, and even if it was, it should show up in the stack trace.

Is there anything in the line of code above that could possible throw an IndexOutOfRange exception?

I know the line will throw an NullReferenceException if GetSafely returns null, and it will also throw an InvalidCastException if it returns anything that cannot be cast to SearchCriteria, but an IndexOutOfRange exception? I'm scratching my head here.

Here is the stack trace:

$LOG--> 2010-06-11 07:01:33,814 [ERROR] SERVERA (14) Web.Global - Index was outside the bounds of the array. 
System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.IndexOutOfRangeException: Index was outside the bounds of the array. 
   at IterateSearchResult(Boolean next) in C:\Projects\Web\UserControls\AccountHeader.ascx.cs:line 242 
   at nextAccountLink_Click(Object sender, EventArgs e) in C:\Projects\Web\UserControls\AccountHeader.ascx.cs:line 232 
A: 

It's hard to say definitively, but I think that the object returned from GetSafely() is what's causing the exception. It looks like some code outside of that method is attempting to iterate over the contents of the returned object, and that's where you're failing.

I think that you need to start looking at the code that uses the object returned from GetSafely().

LBushkin
This line is the last line in the stack trace though. If the exception were coming from anywhere else, wouldn't that be reflected in the stack trace?
Jonathan M
+1  A: 

Weird. I'd say your PBD files are wrong.

Joshua
The PDB timestamp matches the timestamp on the DLL. I thought the same thing, but I don't see any code above or below this line that would also cause that exception.
Jonathan M
Turns out the PDBs were off significantly, after all.
Jonathan M