• Advertisement

Archived

This topic is now archived and is closed to further replies.

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

This topic is 6513 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement


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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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**

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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()

Share this post


Link to post
Share on other sites

  • Advertisement