D
dick
in the "try{throw}catch" structure, how the C++ code return the "type"
thrown by a function?
thrown by a function?
in the "try{throw}catch" structure, how the C++ code return the "type"
thrown by a function?
dick said:in the "try{throw}catch" structure, how the C++ code return the "type"
thrown by a function?
Salt_Peter said:That depends on the function's exception specification.
The function is not "returning" the error, the execution context is
throwing the anomally. Where, if and when the anomally is caught
depends on guarded contexts (try blocks) and their catch blocks.
Thats like asking how does an integer know that its an integer? You
can't do anything in C++ without the type being known. Objects always
know their type or type(s) if inheritance is involved. If you throw a
baseball, the baseball knows its a baseball. If you construct and throw
a std::runtime_error, trust me, the object knows that its a
runtime_error. even if you catch( const std::exception& e) - a
reference to its base, the runtime_error still knows its a
runtime_error.
Alf said:* dick:
Short answer: Magic.
Almost-short answer: the dynamic type of the object throw by 'throw' is
known statically, and is the same as the static type because objects are
thrown by value, and the static type of object caught by 'catch' is
known statically, and is the only thing that matters for whether a
'catch' clause will catch. The linker has access to all this
information. And so it can arrange the necessary tables or global type
identifiers or whatever the implementation is based on (there are two
main ways to do it, but an infinite number of possible ways to do it).
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
dick said:But how the "catch" know the type.
dick said:is there any memory leak when a function throw an object?
dick said:is there any memory leak when a function throw an object?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.