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.
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
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)?
regards,
George
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
Hi CornedBee,
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
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
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; }
regards,
George
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
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?
regards,
George
Wrong. Read my posts again.For non-argument-less throw, it is by the compiler's option whether to throw the original exception object or the temporary object.
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
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?
regards,
George
Compare my statement in #31:
with your statement in #38:There is no original exception object for throw with arguments.
I think even you can figure out what's wrong.2. For non-argument-less throw, it is by the compiler's option whether to throw the original exception object or the temporary object.
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
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?
regards,
George
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