Getting information about where c++ exceptions are thrown inside of catch block?

后端 未结 5 1519
误落风尘
误落风尘 2020-12-13 23:02

I\'ve got a c++ app that wraps large parts of code in try blocks. When I catch exceptions I can return the user to a stable state, which is nice. But I\'m not longer recei

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 23:42

    This is possible with using SEH (structured exception handling). The point is that MSVC implements C++ exceptions via SEH. On the other hand the pure SEH is much more powerful and flexible.

    That's what you should do. Instead of using pure C++ try/catch blocks like this:

    try
    {
        DoSomething();
    } catch(MyExc& exc)
    {
        // process the exception
    }
    

    You should wrap the inner code block DoSomething with the SEH block:

    void DoSomething()
    {
        __try {
            DoSomethingInner();
        }
        __except (DumpExc(GetExceptionInformation()), EXCEPTION_CONTINUE_SEARCH) {
            // never get there
        }
    }
    
    void DumpEx(EXCEPTION_POINTERS* pExc)
    {
        // Call MiniDumpWriteDump to produce the needed dump file
    }
    

    That is, inside the C++ try/catch block we place another raw SEH block, which only dumps all the exceptions without catching them.

    See here for an example of using MiniDumpWriteDump.

提交回复
热议问题