Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Try/Catch block not catching Exception

I have a statement inside a try/catch block, but the exception is not getting caught. Can anyone explain?

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 139:                try
Line 140:                {
Line 141:                    return (int)Session["SelectedLeadID"];
Line 142:                }
Line 143:                catch (Exception ex)

Update This is an ASP.NET application. In the catch block, a new exception is thrown. The code you see is what is displayed on the ASP.NET error page.

like image 376
avesse Avatar asked Jan 24 '23 01:01

avesse


2 Answers

That catch block should catch the exception, but make sure there's no re-throwing in there.

Another small comment: I've been tricked quite a few times by VS, cause it breaks on exceptions like that while running in debug-mode. Try to simply press 'continue' or 'F5' and see if your application doesn't work anyway :)

like image 175
cwap Avatar answered Jan 25 '23 15:01

cwap


I suspect you're going to need to add more detail - that isn't reproducible just from your code. In particular (as already noted) we'd need to see inside the catch, and verify that the exception is actually being thrown from inside the try and not somewhere else.

Other possibilities:

  • you have dodgy code inside the exception handler that is itself throwing an exception
  • you have a dodgy Dispose() that is getting called (using etc)
  • you are in .NET 1.1 and the thing getting thrown (in code not shown) isn't an Exception, but some other object
like image 44
Marc Gravell Avatar answered Jan 25 '23 16:01

Marc Gravell