C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

throw()...Clarification

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
anand
Guest





PostPosted: Thu May 19, 2005 8:54 am    Post subject: throw()...Clarification Reply with quote



Hi

void f();
means that any type of exception may be thrown from the function. If
you say
void f() throw();
it means that no exceptions are thrown from a function

So suppose i write a code like this

#include<iostream>
using namespace std;

class A
{
public:
A(int x1,int y1)Mad(x1),y(y1){} ;
int calc () throw();

private:
int x,y;

};

int A::calc ()
{
int temp;
temp=x-y;
if(temp<0)
throw 0;
return temp;

}

int main()
{
A a(4,5);
try{
a.calc ();
}catch(...)
{
cout<<"Exception"< }
return 0;

}

According to the definition of throw()
calc() function should not throw any exception.
But while executing the above code in am getting "Exception" as
output..
Any ideas...???

I have compiled this program using vc++6.0


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Maciej Sobczak
Guest





PostPosted: Thu May 19, 2005 1:45 pm    Post subject: Re: throw()...Clarification Reply with quote



Hi,

anand wrote:

Quote:
I have compiled this program using vc++6.0

AFAIR, this compiler ignores exception specs.
You code does not compile on g++ 3.2.
After adding throw() to the implementation of A::calc (it is missing in
your code), the code compiles and correctly aborts when run.

--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Me
Guest





PostPosted: Thu May 19, 2005 1:51 pm    Post subject: Re: throw()...Clarification Reply with quote



(code snipped for clarity)
Quote:
class A {
int calc() throw();
};

int A::calc() throw()
{
throw 0;
return 0;
}

According to the definition of throw()
calc() function should not throw any exception.
But while executing the above code in am getting "Exception" as
output..

VC6 ignores throw specifications. It's partially fixed in the latest VC
(http://msdn.microsoft.com/library/en-us/vclang/html/vclrf154functionexceptionspecifiers.asp)
which you can get for free
([url]http://msdn.microsoft.com/visualc/vctoolkit2003/)[/url]. Here is what the
compiler should translate the above code above to (by the way, your
original code should not compile because you were missing a throw()
signature in the function definition):

int A::calc() throw() try {
throw 0;
return 0;
} catch (...) {
std::unexpected();
}

where std::unexpected calls std::terminate by default, which terminates
your program by default. You also seem to be confused about a few
things too. Don't use throw specifications in your code (except when
defining an operator new that returns NULL on failure). It is
considered by some to be bad style, they can disable certain
optimizations depending on your compiler
([url]http://www.boost.org/more/lib_guide.htm#Exception-specification)[/url], and
most importantly, they don't mean what you think they mean (see
[url]http://www.gotw.ca/publications/mill22.htm)[/url]. Don't use catch (...) in
your code, there are only like 2 or 3 specific cases where it's needed.
I encourage you to further read up on C++ and exception handling,
exception safety is not as foolproof as some people seem to believe
they are.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
 


Powered by phpBB © 2001, 2006 phpBB Group