[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: A note to exceptions && some notes to codesize
From:       Johannes Sixt <Johannes.Sixt () telecom ! at>
Date:       1999-06-09 19:42:53
[Download RAW message or body]

On Mon, 07 Jun 1999, Mario Weilguni wrote:
>Somebody mentioned here that exceptions can be used to report "no-more-memory"
>for C++. According to the GNU info for gcc, this is not necessary, since the C++
>standard requires that "new" never returns NULL. 

Have you read the whole story? new never returns NULL because it throws an
exception of type bad_alloc if there's not enough memory! So exceptions *ARE*
used to report out-of-memory situations and exceptions *MUST* be used to
*handle* them!

>* the exception tables are quite big, and there are tons of _throw() calls that
>are never used (Qt uses no exceptions, and the KDE core classes don't use them
>as well). I see no reason having the libraries bloated with this exception code
>if it is unused. Why not enable exception for libraries that use them, and
>disable for all other libraries? I don't think that is a problem.

It *IS* a problem. Just because a library isn't actively using exceptions
doesn't mean that you can compile it without exception support!

Example: The application calls a library function libFunc() which calls back to
the applications via a virtual method (or a function pointer for old-fashioned
people :-) This happens VERY commonly in Qt applications: Just think of all the
event handlers in your applications!  These callbacks can throw exceptions that
can possibly be caught in the function calling libFunc(). In this case
libFunc() must be prepared to clean up due to the exception!

// in library:
void libFunc(void (*cb)())
{
	QString s;
	(*cb)();
	// must clean up s here if cb throws an exception!!
}

// in application:
void f()
{ throw 1; }
int main()
{
	try { libFunc(f); }
	catch (int i) {	}
}


All those extra _throw() calls that you see are needed to clean up the stack if
an exception is thrown. Example:

exter void g(QString);
void f()
{
	QString s1;
	QString s2;
// (1)
	g(s1+s2);
// (2)
}

This function has at least 3 implicit "catch points": at (1) is a catch point
that cleans up if QString::QString for s2 throws; at(2) there's one catch
point that cleans up s1 and s2 if QString::operater+ throws and one catch
point that cleans up s1, s2, and the temporary result of operator+ if g()
throws.

Each of these catch points rethrow the exception after they have finished the
cleanup. Hence the extra _throw()'s.

-- Hannes

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic