catch( ... ) question...

Started by
2 comments, last by Cyberdrek 22 years, 3 months ago
Hey all, as the title mentions, this is a rather basic question but I''m new to using Try, throw, catch. Now, here''s the question, when you use catch(...) to get any the other exceptions that you didn''t treat yourself, is there a way to get the real error message that caused the error? I''m not sure how to proceed. I actually send the errors to a file so I write my own error message but if I want to get the correct error message, is there a function since it''s really catching anything else but my own exception class, I don''t know. "And that''s the bottom line cause I said so!" Cyberdrek Resist Windows XP''s Invasive Production Activation Technology! "gitty up" -- Kramer /(bb|[^b]{2})/ that is the Question -- ThinkGeek.com Hash Bang Slash bin Slash Bash -- #!/bin/bash
[Cyberdrek | ]
Advertisement
You have no access to the exception object thrown while inside a "catch(...)" block. Those are usually used for code that absolutely must catch any and all exceptions, regardless of who throws them (perhaps an API, etc.).

For common use I would recommend deriving from std::exception, which stores a string. Add extra info in your derived classes, but keep the string; then replace your "catch(...)" usage with a "catch(std::exception& e)" block and output the string.

BTW, std::exception is in the file "stdexcept" (no .h extension.
I prefer:
  try{  // code}catch (exception &x){  // handle x}catch (CException *pX){  // for MFC}catch (...){  // no object I know of thrown, but I can print an  // "unknown exception" message instead of just  // crashing}  
Also, in keeping with Stoffel''s example, in your catch(...) block, you can log out an "unknown exception", and then re-throw it using ''throw''. You never know, code above you might need to catch that exception (especially if that code knows how to handle it). If you are just catching and ignoring unknown exceptions, then that can be bad and cause problems. Remember, exceptions are thrown for a reason!


-Brannon
-Brannon

This topic is closed to new replies.

Advertisement