Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Visual Studio break on User (std::exception) Exceptions?

Tags:

My code throws unhandled exceptions, but the debugger in Visual Studio only breaks on those thrown by the system.

For example, below the return value of getaddrinfo is not zero and my exception should be thrown first - in fact, if I place a breakpoint at line 171, it is hit - yet the debugger only breaks on the call to socket.

I know I have to add my own types explicitly, or else check All C++ Exceptions not in this list, in Exception Settings, but this is a std::exception I am throwing, and std::exception is checked.

How do I make the Visual Studio debugger break automatically on my exceptions?

enter image description here

like image 438
sebf Avatar asked Aug 04 '17 11:08

sebf


People also ask

How do I enable Break on exception in Visual Studio?

Tell the debugger to break when an exception is thrownIn the Exception Settings window (Debug > Windows > Exception Settings), expand the node for a category of exceptions, such as Common Language Runtime Exceptions. Then select the check box for a specific exception within that category, such as System.

How do I make Visual Studio not stop on exception?

Note: you can uncheck Break when this exception type is thrown directly in the exception handler and continue debugging (press F5 ). Visual Studio will add this exception type to the Exception settings and will remember that it shouldn't break on this exception again.

How do I fix unhandled exception in Visual Studio?

In Visual Studio, when exceptions are thrown or end up unhandled, the debugger can help you debug these by breaking just like it breaks when a breakpoint is hit.


1 Answers

The debugger has broken on the throw but you're not showing the topmost function in the callstack which is actually raising the exception.

What you're showing is a function further down the stack. And what this shows is that when the function that's currently being called returns the next line to be executed is the socket(...) line. That is why the icon on that line is the little green 'return' icon and not the yellow 'execution is currently here' icon.

Right click on the callstack, click 'show external code' and you'll see something like:

KernelBase.dll!RaiseException(unsigned long dwExceptionCode, unsigned long dwExceptionFlags, unsigned long nNumberOfArguments, const unsigned long * lpArguments) Line 904  C
vcruntime140d.dll!_CxxThrowException(void * pExceptionObject, const _s__ThrowInfo * pThrowInfo) Line 136    C++
ConsoleApplication5.exe!main() Line 6   C++
ConsoleApplication5.exe!invoke_main() Line 64   C++

Note that its the KernelBase.dll!RaiseException where the exception is actually being thrown from.

Yes, I can agree this isn't very c++ like but throwing exceptions is a mechanism which requires complex code and so it happens like this.

like image 116
Mike Vine Avatar answered Oct 11 '22 13:10

Mike Vine