Thread: Re-throwing Exceptions

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445

    Re-throwing Exceptions

    as an example of what I want to do, in C# you can do this:
    Code:
    try
    {
    }
    catch (Exception e)
    {
        // do something with exception
        throw;  // continues propagating the exception without resetting the stack trace
    }
    does C++ have a mechanism like this whereby I can allow the same exception to continue propagating upwards, but handle it at more than one level of the call chain?

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Yes. It does. It is almost identical in C++. The primary difference (I'm guessing as I don't know exactly how it works in C#.) is that in C++ the `catch' variable should always be a reference (&).

    That said, please don't do this. It is a sign of misunderstanding how exceptions best work.

    What are you trying to accomplish?

    Saying "handle it at more than one level" is not sufficient as that statement is flawed. If you handle an exception at one level it has no business being propagated higher as it has already been handled.

    If you need to transform an exception because the error has been handled at a level where the existence of the error signals an entirely different form of failure a different exception should be thrown.

    If you need to add context to an exception, your exception class and point of origin are probably flawed. This can be overlooked if you need to add context to the exception at a higher level because lower level facilities aren't controlled by you.

    If you need to handle a particular flavor of exception by inspecting the attributes of an instance of the exception class you do not have enough exception classes to do the job. The point of the `catch' variable is so that one might handle exactly that error condition. In general, you can assume that one class for each flavor of exception is correct.

    Soma

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    it's not that I have a specific purpose in mind, or that I'm trying to accomplish anything. I recently learned about this feature of C#, and I was just curious to know if it also worked in C++.

  4. #4
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    As phantomotap said, never catch exceptions by value. Always catch them by const reference or you may run into slicing issues (google C++ slicing if you don't know what that means). Anyhow, to rethrow the exception that is currently being handled, a simple

    throw;

    will do.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    the example I gave was in C#, so it catches by reference. in C++ I know to always catch by reference.

  6. #6
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Ok sorry, didn't read the thread carefully enough.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I feel like throwing up
    By happyclown in forum General Discussions
    Replies: 41
    Last Post: 08-10-2010, 10:01 AM
  2. throwing memory around from exe to exe
    By jverkoey in forum C++ Programming
    Replies: 2
    Last Post: 08-26-2004, 09:00 AM
  3. throwing structures
    By subdene in forum C++ Programming
    Replies: 7
    Last Post: 08-08-2004, 04:26 PM
  4. throwing away spaces and \n
    By kippwinger in forum C++ Programming
    Replies: 6
    Last Post: 07-21-2003, 11:57 AM
  5. Throwing exceptions with constructors
    By nickname_changed in forum C++ Programming
    Replies: 14
    Last Post: 07-08-2003, 09:21 AM