throw()..clarification

A

anand

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

Ron Natalie

anand said:
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.
 
R

Rolf Magnus

anand said:
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.
 
B

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
 
R

Rolf Magnus

ben said:
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 ()'
 
A

anand

I have compiled this on VC++ 6.0 and its compiling without errror,
didnt tried with g++.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top