•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 423,108 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,354 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 139 | Replies: 3
![]() |
•
•
Join Date: Jan 2008
Posts: 79
Reputation:
Rep Power: 1
Solved Threads: 5
first model:
second model:
it is clearly that in the second implementation the MyException object is allocated on heap, but how about the first one? What's the mecanism behind it? It would look like it's placed on the stack and then cought by reference, but how can it be placed on the stack if that is what exceptions do, unwind the stack till an apropriate catch clause is found. I mean, the stack pointer drops and when u catch the exception the reference points to an exception object above the current stack pointer... I don't know maybe i'm wrong. Can someone please explain how things actually work?
class MyException
{
string message;
public:
MyException( string msg ): message( msg ) {};
void printmsg() { cout<< message <<endl<<endl; }
};
void willthrow()
{
throw MyException("mk");
}
int main(int argc, char * argv[])
{
try
{
willthrow();
}
catch( MyException &ex)
{
ex.printmsg();
}
}second model:
class MyException
{
string message;
public:
MyException( string msg ): message( msg ) {};
void printmsg() { cout<< message <<endl<<endl; }
};
void willthrow()
{
throw new MyException("mk");
}
int main(int argc, char * argv[])
{
try
{
willthrow();
}
catch( MyException *ex)
{
ex.printmsg();
delete ex;
}
}it is clearly that in the second implementation the MyException object is allocated on heap, but how about the first one? What's the mecanism behind it? It would look like it's placed on the stack and then cought by reference, but how can it be placed on the stack if that is what exceptions do, unwind the stack till an apropriate catch clause is found. I mean, the stack pointer drops and when u catch the exception the reference points to an exception object above the current stack pointer... I don't know maybe i'm wrong. Can someone please explain how things actually work?
See, for example:
http://www.codeproject.com/KB/cpp/exceptionhandler.aspx
http://www.codeproject.com/KB/cpp/exceptionhandler.aspx
>in the second implementation the MyException object
>is allocated on heap, but how about the first one?
It's unspecified:
Anything more detailed than that depends heavily on a specific implementation, so you'll need to choose a compiler you want details for.
>is allocated on heap, but how about the first one?
It's unspecified:
•
•
•
•
Originally Posted by C++0x Draft
3 A throw-expression initializes a temporary object, called the exception object, the type of which is determined by removing any top-level cv-qualifiers from the static type of the operand of throw and adjusting the type from “array of T” or “function returning T” to “pointer to T” or “pointer to function returning T”, respectively. [ Note: the temporary object created for a throw-expression that is a string literal is never of type char*, char16_t*, char32_t*, or wchar_t*; that is, the special conversions for string literals from the types “array of const char”, “array of const char16_t”, “array of const char32_t”, and “array of const wchar_t” to the types “pointer to char”, “pointer to char16_t”, “pointer to char32_t”, and “pointer to wchar_t”, respectively (4.2), are never applied to a throw-expression. —end note ] The temporary is an lvalue and is used to initialize the variable named in the matching handler (15.3). The type of the throw-expression shall not be an incomplete type, or a pointer to an incomplete type other than (possibly cv-qualified) void. Except for these restrictions and the restrictions on type matching mentioned in 15.3, the operand of throw is treated exactly as a function argument in a call (5.2.2) or the operand of a return statement.
4 The memory for the temporary copy of the exception being thrown is allocated in an unspecified way, except as noted in 3.7.3.1. The temporary persists as long as there is a handler being executed for that exception. In particular, if a handler exits by executing a throw; statement, that passes control to another handler for the same exception, so the temporary remains. When the last remaining active handler for the exception exits by any means other than throw; the temporary object is destroyed and the implementation may deallocate the memory for the temporary object; any such deallocation is done in an unspecified way. The destruction occurs immediately after the destruction of the object declared in the exception-declaration in the handler.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Yet another good article:
http://www.codesourcery.com/public/cxx-abi/abi-eh.html
Summary: Exception handling including exception object management is a special (and very interesting) area of C++ compilers design and implementation.
http://www.codesourcery.com/public/cxx-abi/abi-eh.html
Summary: Exception handling including exception object management is a special (and very interesting) area of C++ compilers design and implementation.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: How To Store Multiple object in file inc++
- Next Thread: capturing audio with C++



Linear Mode