7

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.

avesse
  • 347
  • 2
  • 3
  • 8
  • 9
    You haven't shown what's in the catch block - for example, is there a "throw;" statement? – Jon Skeet Oct 27 '09 at 12:24
  • Show the full code of the try/catch block – JL. Oct 27 '09 at 12:35
  • Yes, at the moment it simply throws the exception as the InnerException of a new Exception - logging will be added later. – avesse Oct 27 '09 at 12:40
  • I realise this was a long time ago, but this isn't a nice way to do this you should be using Int32.TryParse which would handle that it might not be able to set the int value. – davoc bradley Mar 08 '16 at 08:52

7 Answers7

18

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 :)

cwap
  • 11,087
  • 8
  • 47
  • 61
  • No, unfortunately it does break. I do re-throw, but I would expect the application to only break further up the call stack? – avesse Oct 27 '09 at 12:42
  • 1
    Remember to rethrow the right way :) http://stackoverflow.com/questions/178456/what-is-the-proper-way-to-re-throw-an-exception-in-c – cwap Oct 27 '09 at 16:34
4

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
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
3

If it is only the debugger breaking on the exception and you are using VS2005 or above, you might want to check under Debug->Exceptions... if any of the Common-Language-Runtime-Exceptions are activated. If so, the debugger will always catch the exceptions first, but you are allowed to continue.

To revert to normal execution, simply uncheck the apropriate exceptions from the list.

Frank Bollack
  • 24,478
  • 5
  • 49
  • 58
0

I had a similar problem where a thrown exception wasn't being caught. In my case it was because I had the error being thrown in an async function that didn't return a Task.

Incorrect Implementation

async void myThrowFunction () {
  await something()
  throw new Exception('foo')
}

Correct Implementation

async Task myThrowFunction () {
  await something()
  throw new Exception('foo')
}
James
  • 3,051
  • 3
  • 29
  • 41
-1

I also faced this problem Image

which was solved by removing the tick of "Break when this exception type is thrown."

Warning: Of course, I am not aware of the consequences of this.

  • this option only affects the debuger. It does not help OP to catch the exception, which is what they are trying to achive. This does not really answer the question or explains even the problem – Mong Zhu Oct 10 '22 at 09:32
-1

I've also had such an issue and it was driving me nuts for quite some time, but in the end I figured it out. It's quite stupid, but maybe it might help someone:

public IActionResult SomeFunction()
{
    try
    {
        return Json(new ClassWhichTakes2Parameters("FirstParameter"), "SecondParameter"); //comma placed in the wrong spot
    }
    catch (Exception ex)
    {
        //some code
    }
}

It should have looked like:

public IActionResult SomeFunction()
{
    try
    {
        return Json(new ClassWhichTakes2Parameters("FirstParameter", "SecondParameter"));
    }
    catch (Exception ex)
    {
        //some code
    }
}

Since I had many return statements in that function I didn't catch this right away.

Also, the error message I've been receiving wasn't quite what I have expected at first, but now it makes sense:

System.InvalidOperationException: Property 'JsonResult.SerializerSettings' must be an instance of type 'Newtonsoft.Json.JsonSerializerSettings'.

josibu
  • 594
  • 1
  • 6
  • 23
-7

The code looks terribly ugly IMO. For there to be something in the catch() block means you are going to have another return ... statement which AFAIK you should always have a single return statement at the end of each function to make following code easier.

i.e. maybe your code should look like

public int Function()
{
  int leadID = 0;
  try
  {
    int leadID = (int)Session["SelectedLeadID"];
  }
  catch (Exception ex)
  {
    ...
  }
  return leadID
}

Single exit points are supposed to make the code easier to follow, I guess? Anyway, to get any useful help you have to post more of the function.

mike
  • 3,146
  • 5
  • 32
  • 46
  • 3
    This is a stylistic thing, there is nothing inherently wrong with returning in a catch block, or having multiple returns (it can even make code simpler by having multiple break points). This is not an answer to the question anyway (I know, I'm very late) – Christian Rondeau Feb 21 '18 at 18:37
  • 3
    You have actually written this with a syntax error. You are redeclaring `leadID` inside the try block. – Zach Ross-Clyne Jul 03 '18 at 11:31
  • Well, having multiple return statements is much better than having an obvious syntax error. (Although there is nothing wrong with any of them.) – Bamdad Jan 19 '21 at 13:05