Is it possible to log exceptions when they are raised? - c++

Visual Studio has this neat feature of popping up a message box whenever an exception is raised allowing you to break on it and inspect the program state even if the exception is theoretically caught later.
I would like to use something like this for logging purposes. Automatically log every exception that is being raised. This would be especially useful in cases where you raise an exception but it is caught in library code even though it should not have been.
Hence the question: Can it be done, if so, how?
Edit: Not sure if this was obvious enough: I do not want to write code to catch, log and rethrow all exceptions everywhere.

You cannot automatically log exceptions that are thrown. You can log exceptions that are created (regardless of whether they are thrown at a later stage) by means of writing your own exception class.
However, I rarely see someone creating an exception without actually throwing that exception; so this might be good enough for you.

Related

C++ how to write something into a logfile just before the programm crashes

everyone, i tried finding a solution on the internet, yet i've failed and i need your help.
I need to write a programm which has the following property:
If it crashes - it should write the time of the crash and the line, where it occured.
I've made a special logging function for this purpose, however - i have no idea how to intercept the crash event and make it write to the logfile - just after the crash occured.
I would really appreciate your help.
You can use std::uncaught_exception in your destructor to check if the stack is unwinding because of an exception and if it's the case write a scoped log entry.
There may also be some interesting information for you in this thread. It contains a lot of useful information about good and bad practice when it comes down to exception handling.
Depends on how you want to implement it.
Let me explain exactly what your trying to catch on a crash.
These are called unhandled exceptions. These are read passed the C++ exception handlers and are considered OS level exception handling. This means your implementation of where and how you catch your errors is OS dependent. (very important)
I only know windows so.... here is the ways you can do it in the windows platform.
three ways.
__try __except -- basically like try catch but lower level
catching signals -- google this if you want there pretty useless in my opinion not going to even bother explaining
my recommend solution is
SetUnhandledExceptionFilter

Find Source of STL::Exception in basic_string

I am trying to track the source of a std::exception, in basic_string::erase, I wrapped all the locations where I am calling erase directly in try/catch and am not seeing any of these catch blocks be hit, so it must be being called internally from another basic_string method. The exception appears to be the result of a race condition in the code i am working with, so it is very difficult to reproduce, any thoughts on how I could detect and or get a stack trace from this exception? btw this is c++ code on an x86 linux box.
Thank you
You should try
(gdb) catch throw
Then gdb will trigger breakpoint each time exception is thrown. You'll see a callstack.
EDIT: This post is a good bunch of tricks for debugging exceptions: GDB: How to break when a specific exception type is thrown?

how to add one exception handler for a class?

I am trying to use one exception handler to handle all exceptions raised by a C++ class. is there a simple way, instead of adding the code to every member function?
Thanks
There's no simple way that you should be doing.
The point of exceptions is that they handle exceptional conditions -- ie: stuff that is unexpected. And that they cause execution to bubble up to code that knows how to handle them. If you have a defined point in your class that should handle all exceptions, either you know they're going to be thrown and want to swallow them (read: you're abusing exceptions) or you don't know they're going to be thrown and just want to swallow them to hide errors (read: you're creating a debugger's nightmare AND abusing exceptions).
If you can't easily handle an exception, just let it be thrown. Don't swallow it, don't pretend it didn't happen.
I completely agree with cHao.
If you just want to make sure that some trivial exception doesn't crash your program, then for example if you are writing a GUI application, you could wrap your event loop in a try/catch block, and then depending on the exception, decide to log it, tell the user, ask for permission to send an error report, make the program shut down (nice if you restart it automatically...), etc. (If it got all the way out of your event loop without being caught, you probably want to know about it!)

Can I stop COM from swallowing uncaught C++ exceptions in the callee process?

I am maintaining a project which uses inter-process COM with C++. At the top level of the callee functions there are try/catch statements directly before the return back through COM. The catch converts any C++ exceptions into custom error codes that are passed back to the caller through the COM layer.
For the purpose of debugging I want to disable this try/catch, and simply let the exception cause a crash in the callee process (as would normally happen with an uncaught C++ exception). Unfortunately for me the COM boundary seems to swallow these uncaught C++ exceptions and I don't get a crash.
Is there a way to change this behaviour in COM? Ie, I want it to allow the uncaught C++ exception to cause a crash in the callee process.
I want this to happen so that I can attach a debugger and see the context in which the exception is thrown. If I simply leave our try/catch in place, and breakpoint on the catch, then the stack has already unwound, and so this is too late for me.
The original "COM masters" who wrote this application are all either unavailable or can't remember enough details.
This just in, a year and a half after the question was asked -
Raymond Chen has written a post about "How to turn off the exception handler that COM 'helpfully' wraps around your server". Seems like the ultimate answer to the question. If not for the OP, for future readers.
I don't think you can disable this behaviour, however there is a way around it if you are using Visual Studio and don't mind getting flooded in exceptions. If you go to Debug>Exceptions in VS and select "When the exception is thrown>Break into the Debugger" for C++ exceptions, it will drop into the debugger at the point the exception is thrown. Unfortunately you then get to work out which exceptions you can ignore and which ones are of interest to you.
The default setting for this is "Continue" with "If the exception is not handled" being set to "break into the debugger". If it doesn't do that already this would suggest that you'll have to find out exactly where the exceptions are being caught.
If I understand correctly, your problem is essentially the inability to get a stack trace from a C++ exception. I've never tried it myself, but it should actually be possible to get the stack trace even from within the catch block.
See: Getting an exception call stack from the catch block
The article describes the process of getting the stack trace using a debugger, but if you don't want one to be attached you can create a dump in the catch clause (one way, another), and then go through the process on your leisure.
Have a look at Vectored Exception Handlers -- depending on your exact use case, VEH could be used to intercept SEH exception handling and force crashes/dumps/whatever.
You can set up a level 2 break in debugger with sxe/sxd -c2 eh that will catch only unhandled C++ exceptions. You can also attach the debugger on the fly to your process at load time using GFlags. Of course you'd have to give up the mickey mouse debugger and use the real deal.
What you need is to enable "break when an exception is thrown" in your debugger. This way you will stop immediately when an exception is thrown and have the entire call stack at your service.

I can't get my debugger to stop breaking on first-chance exceptions

I'm using Visual C++ 2003 to debug a program remotely via TCP/IP.
I had set the Win32 exception c00000005, "Access violation," to break into the debugger when thrown. Then, I set it back to "Use parent setting." The setting for the parent, Win32 Exceptions, is to continue when the exception is thrown.
Now, when I debug the program, it breaks each time that exception is thrown, forcing me to click Continue to let it keep debugging. How do I get it to stop breaking like this?
I'd like to support Will Dean's answer
An access violation sounds like an actual bug in your code. It's not something I'd expect the underlying C/++ Runtime to be throwing and catching internally.
The 'first-chance-exceptions' feature is so you can intercept things which get 'caught' in code, using the debugger, and have a look. If there's nothing 'catching' that exception (which makes sense, why on earth would you catch and ignore access violations?), then it will trigger the debugger regardless of what options you may have set.
Is this an exception that your code would actually handle if you weren't running in the debugger?
Ctrl+Alt+E (or Debug\Exceptions)
From there you can select which exceptions break.