473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

throw()..clarification

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
5 2546
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
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
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
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
I have compiled this on VC++ 6.0 and its compiling without errror,
didnt tried with g++.

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ken | last post by:
Hi. I know it is recommended that exceptions should be caught by reference, but are there recommendations for how an exception should be thrown? Or is it just a matter of throwing a type that is...
8
by: Christian Schuhegger | last post by:
hi, we are working on a c++ project which has a lot of thow list specifications which in the end causes a lot of problems. now i would like to remove them from the project. because the project...
3
by: Pierre Rouleau | last post by:
The std::exception class defined in the Standard C++ <exception> header specifies that the constructors could throw any exception becuase they do not have a throw() specification. Why is that? ...
1
by: Rennie deGraaf | last post by:
I want to use throw() specifiers on some of my methods while debugging, to help me keep track of what is going on, but remove them in release builds. My method of doing this is #ifdef DEBUG...
8
by: Sai Kit Tong | last post by:
In the article, the description for "Modiy DLL That Contains Consumers That Use Managed Code and DLL Exports or Managed Entry Points" suggests the creation of the class ManagedWrapper. If I...
6
by: Arjen | last post by:
Hi, I'm reading the enterprise library documentation and there I see the throw statement. try { // run code } catch(Exception ex) {
18
by: Ranganath | last post by:
Why is throw keyword considered as an operator?
2
by: ravir | last post by:
Hi, I am new to this group. I am working in Perl and shellscripts. I have a clarification regarding perl grep and pattern matching. I am writing a perl script to automate the process of code...
6
by: jason.cipriani | last post by:
Consider this program, which defines a template class who's template parameter is the type of an exception that can be thrown by members of the class: === BEGIN EXAMPLE === #include...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.