5

I'm using Visual Studio 2017 (15.4.1) while developing an ASP.NET Core MVC 2 application running on .NET Framework 4.6.2.

I'm trying to get Visual Studio to break on all unhandled exceptions that pass through my code. How can I get Visual Studio to break on unhandled exceptions that are thrown in an external library that is called from my code?

These are my settings:

  • Exception Settings -> Break when thrown:
    • All defaults. This means:
      • Don't break when thrown for almost every exception.
      • Don't continue when unhandled in user code for almost every exception.
  • Options -> Debugging -> General:
    • Enable Just My Code
    • Use the new Exception Helper.

See the following code snippet:

void ThrowExceptionInUserCode()
{
    // Exception helper doesn't pop up, and shouldn't.
    try { throw new Exception(); }
    catch (Exception) { }

    // Exception helper pops up, and should.
    throw new Exception();
}

void ThrowExceptionInExternalLibrary()
{
    // Exception helper doesn't pop up, and shouldn't.
    try { PopularLibrary.MethodThatFails(); }
    catch (Exception) { }

    // Exception helper doesn't pop up, and should.
    // This is the thing I'm trying to get to work.
    PopularLibrary.MethodThatFails();
}

Note that I cannot disable 'Just My Code', because ASP.NET has a default exception handler, which would make all my exceptions handled.

Edit: Since my question has been marked duplicate, let me specifiy why its not the same as the other question. The other question is more general. 'Why does Visual Studio not break at all on an unhandled exception. This works perfectly fine for me. Visual Studio does break on unhandled exceptions. Visual Studio only does not break on unhandled exceptions thrown from an external library, like my code sample specified.

Secondly the original question has many answers, but non answer the original question. (The original asker even mentions this in the replies.) These other answers solved a different problem for many users, hence the many upvotes.

Nick Muller
  • 2,003
  • 1
  • 16
  • 43
  • Check your `Exception Settings` (`CTRL`+`ALT`+`E`). Is the box `Common Language Runtime Exceptions` checked with a check-mark (so all children are checked)? – Igor Oct 26 '17 at 11:04
  • 1
    @Igor No, but if I check it Visual Studio even breaks when the exception is handled. I only want it to break when the exception is not handled. – Nick Muller Oct 26 '17 at 11:07
  • Ok, I understand. Have you seen this yet? [Visual Studio 2015 break on unhandled exceptions not working](https://stackoverflow.com/questions/31549030/visual-studio-2015-break-on-unhandled-exceptions-not-working) – Igor Oct 26 '17 at 11:08
  • 1
    @Igor Nope, when I try that one Visual Studio will also break on handled exceptions. – Nick Muller Oct 26 '17 at 11:55
  • Sounds like the "Just My Code" setting – AaronLS Nov 15 '17 at 02:51
  • @AaronLS You mean that "Just My Code" should be off? – Nick Muller Nov 16 '17 at 22:32
  • @NickMuller Yes, since you are interested in the exceptions thrown in external libraries, I think you want that setting to be off. You would also have to make sure the Debug->Windows->Exceptions enables those exception types. Yes it will also cause you to break on handled exceptions. – AaronLS Nov 16 '17 at 22:37
  • @AaronLS Doesn’t look look like it should be off. See: https://learn.microsoft.com/en-us/visualstudio/debugger/just-my-code “ .NET Framework Just My Code -> Exception behaviour. – Nick Muller Nov 16 '17 at 22:48
  • @AaronLS And I don’t want it to break on handled exceptions, since that would be really annoying. :) – Nick Muller Nov 16 '17 at 22:49

1 Answers1

0

The problem is that ASP.NET Core catches all exceptions to be able to show an error message to the client rather than crashing the HTTP connection.

So technically all exceptions are "handled" and thus not caught by Visual Studio.

Ideally Visual Studio unhandled exception code would insert itself before the ASP.NET Core exception handler, but so far it doesn't.

hultqvist
  • 17,451
  • 15
  • 64
  • 101