Catch unknown exception

In the release version of my code, one line throws an exception, and I donโ€™t know what type of exception it is, so I cannot catch it correctly or figure out the problem.

I use catch (...), but it is pretty useless.

here is some kind of pseudo code

try { m_mmwIPC = gcnew NiftyIPC(gcnew String("Monitor"), true); } catch (CException* e) { TCHAR szCause[255]; e->GetErrorMessage(szCause, 255); CString errorStr = szCause; RemoveLineFeeds(errorStr); OutputDebugString(errorStr); } catch(...) { OutputDebugString(L"Unknown exception\n"); } 

So, is there any way to get any information about an unknown exception thrown? Just the type will be great.

thanks

+6
source share
5 answers

In fact, it can be an int , const char* or RhubarbPie รผber-smart pointer.

However:

  • Try to catch std::exception too. This will catch many C ++ native exceptions.
  • Your exception is probably .NET, so try to catch this and not the MFC base exception. (It looks like you're doing C ++ / CLI. In this case, .NET exceptions fall into the catch-all clause)
  • In addition, exceptions are usually designed to be detected by reference in C ++ (Update: MFC seems to use a throw-and-catch pointer by pointer. This also works if you catch what is selected. )
  • It may also help to use __try and __catch, as some "hardware" exceptions, such as stack overflows, access violations, etc., are also unknown exceptions on Windows. The syntax for catching them is slightly different, but you get an exception identifier that can be used to report the type of exception that is thrown. I use this to print stack traces for fatal errors in our applications.
+5
source

When you specify the use of MFC, I will assume that you are working with a version of Visual Studio. If so, and you can run your program in debug mode, you can configure the debugger to break unhandled exceptions. This would require removing the catch(...) your code, but it should go into the debugger at the right point and provide you with useful information about the exception itself.

I am referring you to the Microsoft documentation here and here .

+1
source

Each exception should consist of std::exception , then you can use RTTI. Standard catch block

 catch (const std :: exception & e) { // e .what (); // typeid (e); } catch (...) { // WTF ?!?!? } 

In C ++ 0x, you can use std::current_exception and possibly pass exception_ptr to some smart library for analysis.

Keep in mind that exceptions can be buildins and other types that do not have RTTI, so you should always be extracted from std :: exception.

+1
source

No no. catch (...) should only be used as a last resort in fact.

0
source

One option is to not catch the error and run the programmer in the debugger (this is possible in the release mode). Visual Studio will go into the code in which the uncaught exception occurs.

0
source

Source: https://habr.com/ru/post/892323/


All Articles