what is _THROW0 macro ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: what is _THROW0 macro ?

  1. #1
    Join Date
    Oct 2003
    Location
    France
    Posts
    44

    what is _THROW0 macro ?

    Hi Gurus,

    by browsing STL files I saw this _THROW0 strange macro a lot of time at end of methods declaration.

    According to XSTDDEF file:
    Code:
    #define _THROW0()	throw ()
    For instance declaration of release method of auto_ptr is:
    Code:
    _Ty *release() const _THROW0()
    What does it mean ? Is it like Java to specify the method can send exceptions. Anyway it does not seem to be checked by the compiler to verify callers are catching thrown exceptions.

    Any information about this is welcome.
    Thanks

  2. #2
    Join Date
    Sep 2003
    Location
    Forever Gone... For Now...
    Posts
    1,515
    Is it like Java to specify the method can send exceptions. Anyway it does not seem to be checked by the compiler to verify callers are catching thrown exceptions.
    Bing! We have a winner!

    MSDN, "Exception Specifications"
    The following table summarizes Visual C++'s implementation of exception specifications:
    Code:
    Exception specification		Meaning 
    throw()				The function does not throw an exception. 
    throw(...)			The function can throw an exception. 
    throw(type) 			The function can throw an exception of type type. However, in Visual C++ .NET, this is equivalent to throw(...).
    Thought for the day/week/month/year:
    Windows System Error 4006:
    Replication with a nonconfigured partner is not allowed.

  3. #3
    Join Date
    Sep 2003
    Location
    Forever Gone... For Now...
    Posts
    1,515
    Apologize for quoting MSDN in the "C++ (Non Visual C++ Issues)" forum...
    Thought for the day/week/month/year:
    Windows System Error 4006:
    Replication with a nonconfigured partner is not allowed.

  4. #4
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    Just be careful when using this in your own methods. In fact, don't unless you are very very certain that your method can't throw. The compiler has no way to determine if a method satisfies this requirement, and if it does happen to throw, the program will not pass go, it will not collect $200. It will call terminate().

    Jeff

  5. #5
    Join Date
    Oct 2003
    Location
    France
    Posts
    44
    All right,

    here is what I tried:
    Code:
    class CDummy 
    {
    public:
    	CDummy() {};
    	virtual ~CDummy() {};
    	
    	virtual void doSomething() throw(exception) {
    		throw new exception();
    	}
    
    };
    
    void doSomethingElse() {
    	CDummy oneDummy;
    	oneDummy.doSomething();
    }
    And the compiler does not complain about method doSomethingElse does not catch exception. Why ?

  6. #6
    Join Date
    Oct 2003
    Location
    France
    Posts
    44
    By the way, I did not succeed to compile a throw(...). Any idea about this ?

    What is the goal of these throw methods declaration if never checked by compiler ? Is it a kind of "future use" functionalities, is it just to decorate the code ?

  7. #7
    Join Date
    Sep 2003
    Location
    Forever Gone... For Now...
    Posts
    1,515
    What compiler are you using? The doc I posted was for VC++ 7.1.

    Again, MSDN (Oct 2003):
    "C++ Language Reference", "Nonstandard Behavior":
    The following topics are some of the known places where the Visual C++ implementation of C++ does not agree with the C++ standard. The section numbers refer to section numbers in the C++ standard.
    ...
    15.4 Function Exception Specifiers
    Function exception specifiers other than throw() are parsed but not used. For example:

    void f() throw(int); // parsed but not used
    void g() throw(); // parsed and used

    For an excellent discussion on this topic, may I recommend the following MSDN article (I'm a MSDN addict):
    "Handling Exceptions, Part 11" by Robert Schmidt
    Actually, the entire "Handling Exceptions" series is worth several reads...
    Thought for the day/week/month/year:
    Windows System Error 4006:
    Replication with a nonconfigured partner is not allowed.

  8. #8
    Join Date
    Oct 2003
    Location
    France
    Posts
    44
    Thanks vicodin451 for your answers.
    What compiler are you using? The doc I posted was for VC++ 7.1.
    I am using a 6.0
    For an excellent discussion on this topic, may I recommend the following MSDN article (I'm a MSDN addict):
    Thanks I will read this.

  9. #9
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    The caller is not required to catch what a method throws. There is no such rule in C++.

    I'd strongly suggest not doing this. There is very little benefit, and very serious consequences if your method throws something other than what you specify.

    Also there's no reason to have throw(...). Every method that does not specify anything is assumed to be throw(...), meaning it may throw anything.

    You really need to read Herb Sutter's Exceptional C++ before venturing further. You need to understand this stuff before you use it or it will bite you.

    Jeff

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center