Thread: What can be done with catch(...)?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    What can be done with catch(...)?

    A couple questions about catch(...):

    1. Is there anything it doesn't catch/stop?
    2. Is there any info you can get on the error that happened?
    3. Is there a way to do some processing inside that catch block and then rethrow the error?

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by 6tr6tr View Post
    A couple questions about catch(...):

    1. Is there anything it doesn't catch/stop?
    2. Is there any info you can get on the error that happened?
    3. Is there a way to do some processing inside that catch block and then rethrow the error?
    It will catch anything, but you can't get any info from it. I would only have this at the very top level of a program, where I would log that an unknown exception was caught and then exit the program.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by medievalelks View Post
    It will catch anything, but you can't get any info from it. I would only have this at the very top level of a program, where I would log that an unknown exception was caught and then exit the program.
    It will catch anything that is catchable. I've seen some things were caught on Windows but not on Solaris with catch (...) - I think it was either divide by 0 or dereferencing a NULL pointer, I don't remember which?

    And you can re-throw an exception like this:
    Code:
    catch (...)
    {
       throw;
    }

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by cpjust View Post
    It will catch anything that is catchable. I've seen some things were caught on Windows but not on Solaris with catch (...) - I think it was either divide by 0 or dereferencing a NULL pointer, I don't remember which?

    And you can re-throw an exception like this:
    Code:
    catch (...)
    {
       throw;
    }
    Thanks!

    Yeah, I figured there were a few things that were uncatchable. So in a case like that, what can you do?

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by cpjust View Post
    It will catch anything that is catchable. I've seen some things were caught on Windows but not on Solaris with catch (...) - I think it was either divide by 0 or dereferencing a NULL pointer, I don't remember which?
    Yes, sorry. It was implied that it will catch only things that are thrown. Dereferencing a null pointer will cause a segmentation violation.
    Last edited by medievalelks; 04-17-2008 at 08:31 AM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by medievalelks View Post
    Yes, sorry. It was implied that it will catch only things that are thrown. Dereferencing a null pointer will cause a segmentation violation.
    And the default setting for Windows is that Structured Exception Handling causes a throw in C++, so in Windows you (probably) will catch for example segfaults.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The standard says that catch(...) catches every C++ exception, i.e. everything that was thrown by a throw statement or (in C++0x) by the rethrow_exception() function.

    Some implementations unwisely extended this to catch non-C++ exceptions, too.


    catch(...) is for two situations:
    1) You want to catch everything, do some processing (typically cleanup), and then rethrow. This is usually better achieved using destructors of local objects, but sometimes you have no choice.
    2) There's a place where you cannot let exceptions pass. This may be the bottom layer of your application, where you'd rather print a message and continue instead of crashing, or it may be at the interface to some non-C++ code that cannot handle exceptions.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. How do I catch an exception thrown from a linked DLL?
    By 6tr6tr in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2008, 07:49 AM
  3. Is there a catch to return in a try/catch box?
    By meili100 in forum C++ Programming
    Replies: 25
    Last Post: 11-21-2007, 01:33 PM
  4. Need advice: catch exceptions or call methods to check bits?
    By registering in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2003, 01:49 PM
  5. Try...catch and XML
    By nickname_changed in forum C++ Programming
    Replies: 11
    Last Post: 07-07-2003, 02:02 AM