Advertisement

What is the throw() syntax in a function declaration?

Started by April 18, 2000 08:11 AM
6 comments, last by null_pointer 24 years, 8 months ago
What is the throw() syntax in a function declaration, and how is it used/what does it affect? - null_pointer Sabre Multimedia


using throw(...) as part of the function declaration indicates what types of exceptions the function is capable of throwing, if any:

void myfunc() throw(int); //: throws only integer execeptions
void myfunc() throw(int, char); //: throws only int & char
void myfunc() throw(myException); //: throws myException
void myfunc() throw(); //: throws NO exceptions

I wasn''t familar with the syntax myself...this is pretty much verbatim from "C++: The Complete Reference", by Schildt.

Z
__________________________________________

Yeah, sure... we are laughing WITH you ...
Advertisement
It''s intended as an optimization hint for the compiler. If you indicate that a function can''t throw any exceptions (that includes any functions called within it) then the compiler doesn''t need to keep track of the exception bookkeeping information from that point down. How much of a savings this creates, I''m not really sure. I guess it depends on how much overhead exceptions add to stack management. It also helps because it adds information to the declaration of the fnuctions. If you see a function declared like:

void someLibraryFunc() throw(libraryIOException);

You KNOW that when you use that function from that library, you ONLY have to worry about IO expcetions, not any of the other errors that might occur. This isn''t always a huge benefit, but in a very large library (say a wrapper for all of DirectX) it could pay off in spades.

-Brian
I can''t believe I missed this! Thanks for telling me about it!

Does anyone know exactly how much overhead exception handling introduces to each particular function?


- null_pointer
Sabre Multimedia
As an example, here are some results from a review of C++ compilers conducted in 1995. The test program created and destroyed a large number of local objects with associated constructors and destructors. No actual exceptions occurred, and the difference in the two programs was the presence of a single catch(...) statement within main().

object size difference with and without EH
Borland 3%
Microsoft 13%
Symantec 8%

execution speed difference
Borland 6%
Microsoft 5%
Symantec 4%

I''m not sure if those numbers are accurate nowadays, but at least they give an impression.
(taken from ''Inside the C++ Object Model'', by Stanley Lippman)

Erik
Interesting stuff! That''s not that big a performance hit for much more useable and robust code.


#pragma DWIM // Do What I Mean!
~ Mad Keith ~
**I use Software Mode**
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
Advertisement
I also wouldn''t be surprised if those numbers have improved (shrunk) in the meantime. People are ALWAYS looking for ways to make exceptions be less intrusive when they aren''t happening. Ideally, they''d be zero overhead until an exception actually happened, but that''s probably not possible...

-Brian
Just thought it might be worth mentioning, but VC++6 has not implemented the exception specification(stating what exceptions a method can throw) yet. I get countless warnings when I do throw()

This topic is closed to new replies.

Advertisement