Thread: life cycle of exception object

  1. #31
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There is no original exception object for throw with arguments. The standard specifies that a copy of the argument to throw is thrown, but also that the compiler may elide the copy.
    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

  2. #32
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    It is interesting to learn it and correct my error senses before. :-)

    So, you mean when we call throw, nothing at all is thrown and even if we use catch to catch the exception, we can not use like what() methods on it (because of no exception object is thrown)?

    Quote Originally Posted by CornedBee View Post
    There is no original exception object for throw with arguments. The standard specifies that a copy of the argument to throw is thrown, but also that the compiler may elide the copy.

    regards,
    George

  3. #33
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Huh? How do you consistently manage to twist my statements into something I don't understand myself?

    If nothing at all was thrown, how would you be able to catch something?

    No, I'm saying that the term "original exception object" is meaningless when talking about the argument version of throw, because - unless we're talking about throwing while stack unwinding is already in progress - there is no such thing. The only exception object is the one that you're about to throw, which is a copy of the argument to the throw statement. Unless the compiler decides not to create your temporary argument and copy it to the exception object storage, but instead uses your temporary construction arguments to directly construct the exception object, i.e. it elides the copy.
    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

  4. #34
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi CornedBee,


    Quote Originally Posted by CornedBee View Post
    No, I'm saying that the term "original exception object" is meaningless when talking about the argument version of throw, because - unless we're talking about throwing while stack unwinding is already in progress - there is no such thing.
    I think you should say,

    "is meaningless when talking about the argument-less version of throw" other than "is meaningless when talking about the argument version of throw". From our recent posts, we are all talking about throw without argument, right?


    regards,
    George

  5. #35
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    NO!

    In the case of argument-less throw, the term is meaningful: it's the exception currently being handled.
    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

  6. #36
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    1.

    I have made a sample program and found it is my mistake to misunderstand throw without any argument. If there is no exception (we are currently not in exception handling status), and invoking throw will cause application termination, right? Like I showed in my program func1;

    2.

    I have posted my code below to show your idea, when invoking throw, the same exception will be thrown and no temporary object is copied? Please help to review whether my code is corret. Thanks!

    Output:

    catch MyException in main
    200

    Code:
    #include <iostream>
    
    using namespace std;
    class MyException
    {
    public:
    	int errorcode;
    	MyException (int input): errorcode(input)
    	{
    	}
    
    	MyException (const MyException& e) // copy constructor
    	{
    		this->errorcode = e.errorcode;
    		cout << "in copy constructor" << endl;
    	}
    };
    
    int func1()
    {
    	try{
    		throw;
    	}catch(...)
    	{
    		cout << "catch in func1 " << endl;
    	}
    
    	return 0;
    }
    
    int func2()
    {
    	try{
    		throw MyException(200);
    	}catch(MyException& e)
    	{
    		throw;
    	}
    
    	return 0;
    }
    
    int main()
    {
    	try{
    		func2();
    	} catch (MyException& e)
    	{
    		cout << "catch MyException in main " << endl;
    		cout << e.errorcode << endl;
    	}
    
    	return 0;
    }
    Quote Originally Posted by CornedBee View Post
    NO!

    In the case of argument-less throw, the term is meaningful: it's the exception currently being handled.

    regards,
    George

  7. #37
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    1) Correct.

    2) I give up.
    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

  8. #38
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    Let me give a simple conclusion of this long discussion,

    1. For arguement-less throw, the same object will be thrown;
    2. For non-argument-less throw, it is by the compiler's option whether to throw the original exception object or the temporary object.

    Right?

    Quote Originally Posted by CornedBee View Post
    1) Correct.

    2) I give up.

    regards,
    George

  9. #39
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    For non-argument-less throw, it is by the compiler's option whether to throw the original exception object or the temporary object.
    Wrong. Read my posts again.
    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

  10. #40
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Why I am wrong, CornedBee?


    This is your post #31

    #31
    There is no original exception object for throw with arguments. The standard specifies that a copy of the argument to throw is thrown, but also that the compiler may elide the copy.

    This is what I mentioned in post #38

    2. For non-argument-less throw, it is by the compiler's option whether to throw the original exception object or the temporary object.

    I think we are talking about the same thing, right?

    Quote Originally Posted by CornedBee View Post
    Wrong. Read my posts again.

    regards,
    George

  11. #41
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Compare my statement in #31:
    There is no original exception object for throw with arguments.
    with your statement in #38:
    2. For non-argument-less throw, it is by the compiler's option whether to throw the original exception object or the temporary object.
    I think even you can figure out what's wrong.
    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

  12. #42
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    My mistake, CornedBee!


    My question is answered.

    Quote Originally Posted by CornedBee View Post
    Compare my statement in #31:

    with your statement in #38:

    I think even you can figure out what's wrong.

    regards,
    George

  13. #43
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Sorry for interrupting again, CornedBee. :-)


    I read the whole discussion today again. I am confused what do you mean no original exception object in argument format of throw and why there is no original exception object, for example,

    MyException e;
    throw e; // suppose this statement is the first throw statement in the exception-throwing catching, and handling chain

    I think e is the original exception object, right? Why do you say there is no original exception object?

    Quote Originally Posted by CornedBee View Post
    Compare my statement in #31:

    with your statement in #38:

    I think even you can figure out what's wrong.

    regards,
    George

  14. #44
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Because by exception object, I understand an object that is within the exception handling machinery. That's not the case here. e is a simple local variable.
    The "original" exception object is the (conceptual, as it may be elided) copy of e that is actually thrown. But because it's also the only exception object, the attribute "original" is pointless.

    Of course, if by exception object you understand any object of an exception class type, the situation changes. But I don't find this terminology useful. Especially as, unlike in Java, there is no language rule about what's an exception class and what's not. In C++, any type is throwable, even built-in types, and it's only convention to derive exception classes from std::exception, or perhaps MFC's CException, or whatever exception base class your framework of choice provides. To contrast, in Java there's java.lang.Throwable, and you cannot throw anything that isn't derived from it.
    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. Telling a shared_ptr not to delete object?
    By TriKri in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2008, 04:26 AM
  2. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  3. Problem with the exception class in MINGW
    By indigo0086 in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2007, 01:12 PM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  5. Life, The Universe, and everything else
    By ZooTrigger1191 in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 03-29-2003, 05:33 PM