7

When I'm debugging my project and I encounter a runtime error the debugger stops on the main line instead of the error line.

using AppKit;

namespace Project {
    static class MainClass {
        static void Main(string[] args) {
            NSApplication.Init();
            NSApplication.Main(args); // breaks here
        }
    }
}

Instead of error line:

if (isTrue) {
    button.Title = "Title"; // Object reference not set to an instance of an object
}

Is there a way to change this so it breaks on the line that has the error?

enter image description here

enter image description here enter image description here I tried the Ignore option:

enter image description here

However, it is unchecked the next time around:

enter image description here

1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231
  • I am guessing that code that throws an exception is not in a try catch, otherwise you could put a breakpoint in the catch. On VS Mac you could try creating an exception catchpoint by opening the Breakpoints window, then clicking New Exception Catchpoint and specifying the exception in that dialog. There is also a preview feature in preferences Other - Preview Features - Ability to ignore first-chance exceptions by location or type, which might be another thing to try here. – Matt Ward Jun 10 '21 at 09:47
  • 1
    I tried and selected "Ignore" and it still lands on the same spot – 1.21 gigawatts Jun 11 '21 at 12:38
  • FWIW: "ignore" is the opposite of what is wanted here. If it makes it to the MainClass, then its already "too late" to stop at the exception line. – ToolmakerSteve Aug 01 '22 at 19:06

2 Answers2

8

This worked for me in Visual Studio for Mac:

  1. Go to the main menu View -> Debug Windows -> Breakpoints

  2. Click the New Exception Catchpoint button (or Breakpoint instead of Catchpoint)

  3. Leave the options pretty much as they are, i.e.:

    a) Breakpoint Action = Pause the program

    b) When to Take Action = When an exception is thrown ("System.Exception"; Include subclasses = true)

  4. Confirm by clicking the Create button

  5. That's it. Start the project (the main menu Run -> Start Debugging or just hit the play button in the toolbar) and enjoy catching the errors

Ivan Sivak
  • 7,178
  • 3
  • 36
  • 42
0

That depends. Certainly the easiest way (if you know you're looking for a particular error) is to enable (check) the corresponding exception in the exceptions window (Debug->Windows->Exception settings->Common Language Exceptions). Since NREs are nothing you would expect at runtime, this works quite ok for these.

What you want is typically the default behavior, if the exception is unhandled and on the main thread. If the exception happens in a task or async method, things truly get a bit ugly, because then the exception is first wrapped to an AggregateException which is later thrown to the main thread when the task is awaited for.

When you get that undesired behavior, do you still have the correct location in the call stack? You might also need to disable "just my code" if the exception happens in a different module.

PMF
  • 14,535
  • 3
  • 23
  • 49
  • Here's what I see on the Debugging preferences pane (added image to post). – 1.21 gigawatts Jun 08 '21 at 09:14
  • Oh, I was not seeing that you're working on a mac, but I'm confused as to why this is that different. C# development should be the same regardless of platform. Do you have different options available _while_ the debugger is active? – PMF Jun 08 '21 at 11:49
  • @PMF It's different, because the Mac IDE evolved from Mono. It's not a port of Visual Studio sadly. It's just a sad version of VS. – Legacy Code Jan 22 '23 at 04:52