By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,696 Members | 1,486 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,696 IT Pros & Developers. It's quick & easy.

throw()..clarification

P: n/a
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):x(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"<<endl;
}
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...???

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
anand wrote:
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
Yes.
So suppose i write a code like this

#include<iostream>
using namespace std;

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

private:
int x,y;

};

int A::calc ()
This shouldn't compile, since the exception specification needs to be the
same in the definition as in the declaration.
{
int temp;
temp=x-y;
if(temp<0)
throw 0;
return temp;
}

int main()
{
A a(4,5);
try{
a.calc ();
}catch(...)
{
cout<<"Exception"<<endl;
}
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...???


Jul 23 '05 #2

P: n/a
anand wrote:
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
Actually, it means the implementation will die rather than allowing
an exception to escape from that function.

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...???


What should happen is the function unexpected() should be called.
While various things can happen in unexpected(), allowing an exception
to escape from f() is NOT one of them.
Jul 23 '05 #3

P: n/a
ben
This shouldn't compile, since the exception specification needs to be the
same in the definition as in the declaration.

It really shouldn't, but it does. Visual C++ 7.1 only issues a warning, g++
doesn't even give a warning.

ben
Jul 23 '05 #4

P: n/a
ben wrote:
This shouldn't compile, since the exception specification needs to be the
same in the definition as in the declaration.

It really shouldn't, but it does. Visual C++ 7.1 only issues a warning,
g++ doesn't even give a warning.


Uhm, I actually found that out by trying to compile it with g++, which gave
me the following error:

exceptions.cpp:16: error: declaration of `int A::calc()' throws different
exceptions
exceptions.cpp:8: error: than previous declaration `int A::calc() throw ()'

Jul 23 '05 #5

P: n/a
I have compiled this on VC++ 6.0 and its compiling without errror,
didnt tried with g++.

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.