Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cause C++ throw to dump core if the exception would be handled by a particular catch block

Is there a way to cause a throw in C++ to dump core at the throw site if the thrown exception would be handled by a certain catch block? I would like something similar to what happens with g++ when an exception reaches the top level.

For example, I would like something like this:

try {
  bar();
  try {
    foo();
  } catch(...) {
#  pragma dump_at_throw_site
  }
} catch(...) {
  std::cerr << "There was a problem" << std::endl;
}

This way, if any exception thrown from foo() or its callee's that reaches the call-site of foo() would cause a core dump at the throw site so one can see who threw the exception that made it to the to this level.

On the other hand, exceptions thrown by bar() would be handled normally.

like image 530
Manish Avatar asked Sep 05 '12 23:09

Manish


People also ask

What will happen if an exception is thrown for which no matching catch () block is defined?

If there is no catch block at the current scope matching the thrown exception, the current scope is exited, and all automatic (local nonstatic) objects defined in that scope are destroyed. The surrounding scope (which might be function scope) is checked for a matching handler.

Can we throw exception from catch block in CPP?

C++ try and catchThe throw keyword throws an exception when a problem is detected, which lets us create a custom error. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

What happens when an exception object is not caught and handled properly?

What happens if an exception is not caught? If an exception is not caught (with a catch block), the runtime system will abort the program (i.e. crash) and an exception message will print to the console.

What is throw in exception handling in C++?

An exception in C++ is thrown by using the throw keyword from inside the try block. The throw keyword allows the programmer to define custom exceptions. Exception handlers in C++ are declared with the catch keyword, which is placed immediately after the try block.


1 Answers

Yes,it can in Windows. I don't know Linux, suppose it can also.

We can register a Exception Handler function to response the throw before the catch Here is the code example:

#include <iostream>
#include "windows.h"
#define CALL_FIRST 1
LONG WINAPI
VectoredHandler(
    struct _EXCEPTION_POINTERS *ExceptionInfo
    )
{
    UNREFERENCED_PARAMETER(ExceptionInfo);
    std::cout <<"VectoredHandler"<<std::endl;
    return EXCEPTION_CONTINUE_SEARCH;
}
int main()
{
    PVOID handler;
    handler = AddVectoredExceptionHandler(CALL_FIRST,VectoredHandler);

    try {
        throw 1;
    }catch(...)
    {
        std::cout <<"catch (...)"<< std::endl;
    }

    RemoveVectoredExceptionHandler(handler);
    std::cout << "end of main"<<std::endl;
    return 0;
}

The outputs of code are:

VectoredHandler
catch (...)
end of main

So,you can dump core int the function VectoredHandler. The VectoredHandler is called after the debugger gets a first chance notification, but before the system begins unwinding the stack. And if your purpose is just to debug the problem issue, then you can rely on the debugger feature to handle the first chance exception, don't need dump the application.

For your information, you may need know What is a First Chance Exception? in windows to understand how windows dispatch the exception.

like image 197
RolandXu Avatar answered Oct 05 '22 04:10

RolandXu