Home > Net >  Can I get Visual Studio 2019 to show me what line of code runtime errors occur on in C ?
Can I get Visual Studio 2019 to show me what line of code runtime errors occur on in C ?

Time:09-16

I'm specifically asking about runtime errors, as in errors that occur during or after the execution of a program, not errors with the C Runtime library or compiler errors. The menu is impossible to figure out, I can't find tutorials that tell me this, and all I've managed is running it in basic debug mode to tell me what kind of error got thrown.

CodePudding user response:

It's not entirely clear from your question what you mean by 'runtime error', but you can make the Visual Studio debugger 'break' on specific C exceptions which will stop at the 'throw' point.

For Visual Studio 2019, this is done via Debug -> Windows -> Exception Settings

By default, it does not break when std::exception derived exceptions are thrown, but you can set it in the UI:

Exceptions Settings.

Note that in some cases you'll get a breakpoint within a component when an exception is first thrown, but if you continue the code recovers. For example, this can happen inside the DirectX Runtime.

See Microsoft Docs.

CodePudding user response:

You have 2 choices really. The first is logging. Write to a file, run your program with a more verbose debug level and write out lines that show every function you enter. You can narrow things down then.

That's a crude way of debugging program errors, reliable though.

the second way, on Windows, is to make use of pdb - program database - files. these are generated by Visual Studio as part of debug builds, and can be generated by release builds too. The vitally important part here is that your program that you are running matches the pdb that was generated at the same time. I cannot sress this enough.

Once you have this you can turn on runtime crash handling (or better still, enable it in code during main) and then, when a program crashes it will write out a "minilog" file that contains the program state at the crash. You can load this into VS (or windbg) to them see the state of your program as it crashed - including the line of code it crashed at.

You can use a source server to make matching your program code with the crashed process easier.

Its not as complex as it first appears, and it amazingly good at handling crashes on client sites, they just have to send you the minilog dump file, and you can debug exactly what happened. The Windows developers did an awesome job with this.

  • Related