C ++ retrieves exception information

I have a C ++ dll that I need to debug. Due to the circumstances in which I use the dll, I cannot debug it through the calling application.

So, I created try -catch where catch writes the exception to the file.

The line to be debugged includes imported classes from third-party DLLs, so I don’t know what type of exception it is. When I tried to catch (exception e), not a single message was written to the file. So I tried to catch (...) what caused something:

using std :: exception :: what, the only thing written to the file was "1". using std :: exception :: exception, the file received the following code: "0579EF90".

Is there a way to get useful information about the exception that was selected?

TIA

Cg

+4
source share
5 answers

If you do not use catch(KnownExceptionType ex) and use your knwoledge about KnownExceptionType to retrieve information, you cannot.

When you catch using catch(...) , you are pretty much lost, you know that you handled the exception, but there is no type information there, you can do little.

You are in the worst case, the exception leaves the library, you do not have information about the exception, even if you have headers for lib, this type of exception does not need to be defined there.

+7
source

maybe try to catch std :: exception and e

  • std :: cout <e.what () <cps;
  • see if you can pass it to std :: logic_error and std :: runtime_error - this should give you some idea of ​​what you mean
+3
source

If you understand correctly, you have already narrowed down the source of the problem to a specific call to a third-party library, but you are not allowed to debug the application live (I want to ask why?), And your question: "How can I debug an exception without knowing what an exception?

Answer: you cannot. As you have noticed, you can blindly guess and hope to catch the right things. You can also catch (...), but that certainly won't tell you. If you can debug live, you can set the debugger to break when an exception is thrown and see what happens there.

I believe that the correct answer is to contact a third-party library where you narrowed down the source of the problem and ask them. Very, very poor form to throw an exception and allow it to propagate across modules. This makes me suspect that this is a Windows SEH exception for the deref null pointer or something else, and you are compiling in such a way that catch (...) catches them.

+3
source

First, you should always catch exceptions using the const reference, in other words:

 catch( const std::exception & ex ) { ... } 

Not doing this means that your exceptions will be of the exact type that you catch, and this may lead to loss of information about the exception.

However, it seems that your library is throwing something not obtained from std :: exception - you need to find out what a type is (ideally a base type).

+1
source

I am a bit confused. On the one hand, you write catch(std::exception) did not work (you should use catch(const std::exception&) , BTW), on the other hand, you also wrote you called std::exception::what() . How did you do this if you did not have std::exception in the first place?

In any case, once you have caught anything other than ... , you can try to register the RTTI information:

 #include <typeinfo> try { foreign_code(my_data); } catch(const some_type& x) { std::cerr << "Yikes! Caught exception of type '" << typeid(x).name() << "' with its hand in the cookie jar!\n"; std::abort(); } 

While the standard makes no assumptions about the result of std::type_info::name() , most (if not all) compilers will generate code that emits what is at least useful.

When you are in the VS debugger, you can also configure it so that it stops under any exceptions. This gives you a stack trace and thus can give you an idea of ​​what data passed to the DLL might cause the problem.

+1
source

All Articles