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...??? 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...???
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. 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
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 ()'
I have compiled this on VC++ 6.0 and its compiling without errror,
didnt tried with g++. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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? ...
|
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...
|
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...
|
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)
{
|
by: Ranganath |
last post by:
Why is throw keyword considered as an operator?
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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,...
| |