Can this line of code really throw an IndexOutOfRange exception?
- by Jonathan M
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:
SearchCriteria is an Enum with only two values
Session is the HttpSessionState
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; }
}
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