473,659 Members | 2,666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

throw operator

Why is throw keyword considered as an operator?

Oct 3 '07 #1
18 3223
Ranganath wrote:
Why is throw keyword considered as an operator?
It is impossible to answer a question based on a wrong premise.
'throw' is not an operator. Why do you think it is one? Or,
rather, what do you mean "considered as an operator"? What is
the context?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 3 '07 #2
On 2007-10-03 12:31, Ranganath wrote:
Why is throw keyword considered as an operator?
For the same reason that new, delete, and sizeof are operators I would
suspect. What else would it be, if it was not an operator?

--
Erik Wikström
Oct 3 '07 #3
Erik Wikström wrote:
On 2007-10-03 12:31, Ranganath wrote:
>Why is throw keyword considered as an operator?

For the same reason that new, delete, and sizeof are operators I would
suspect. What else would it be, if it was not an operator?
What's 'return'? 'throw' is exactly that.

'delete' is an operator because you're allowed to overload it and
because the name of that function would be 'operator delete'. But
among the rest of true operators it stands out because it does not
yield a value and hence cannot be used in another expression.

'throw' is a keyword used in two constructs: to declare the list
of exceptions the function can throw and to actually redirect the
flow. 'return' isn't used to declare anything, neither is 'goto'.
But 'throw' is much more like 'return' or 'goto' than 'sizeof' or
'typeid'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 3 '07 #4
On Oct 3, 8:46 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
Ranganath wrote:
Why is throw keyword considered as an operator?

It is impossible to answer a question based on a wrong premise.
'throw' is not an operator. Why do you think it is one? Or,
rather, what do you mean "considered as an operator"? What is
the context?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
throw is an operator. Please have a look at http://en.wikipedia.org/wiki/Operators_in_C_and_C++.
All I wanted to know is what constraint makes it to be treated as an
operator.

Oct 4 '07 #5
Ranganath wrote:
On Oct 3, 8:46 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
>Ranganath wrote:
Why is throw keyword considered as an operator?

It is impossible to answer a question based on a wrong premise.
'throw' is not an operator. Why do you think it is one? Or,
rather, what do you mean "considered as an operator"? What is
the context?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

throw is an operator.
According to the language standard, "throw" is a keyword, not an operator.
Please have a look at clauses [2.11] (listing all keywords) and [2.12]
(listing all operators and punctuators).

Please have a look at http://en.wikipedia.org/wiki/Operators_in_C_and_C++.
That would be a either a bug in wikipedia or may indicate that wikipedia
uses a different notion of "operator" (maybe something that aims to be
meaningful across programming languages, but I am just guessing here).
All I wanted to know
is what constraint makes it to be treated as an operator.
Huh?
Best

Kai-Uwe Bux
Oct 4 '07 #6
On Oct 3, 7:16 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
Erik Wikström wrote:
On 2007-10-03 12:31, Ranganath wrote:
Why is throw keyword considered as an operator?
For the same reason that new, delete, and sizeof are operators I would
suspect. What else would it be, if it was not an operator?
What's 'return'? 'throw' is exactly that.
Not really. Throw can be used in an expression, return can't.
'delete' is an operator because you're allowed to overload it and
because the name of that function would be 'operator delete'. But
among the rest of true operators it stands out because it does not
yield a value and hence cannot be used in another expression.
You're forgetting the comma operator, and ?:. Things like:

MyClass::MyClas s( T arg )
: myMember( isValid( arg ) ? arg : throw "Invalid arg")
{
}

are perfectly valid and legal.
'throw' is a keyword used in two constructs: to declare the list
of exceptions the function can throw and to actually redirect the
flow. 'return' isn't used to declare anything, neither is 'goto'.
But 'throw' is much more like 'return' or 'goto' than 'sizeof' or
'typeid'.
Throw can be used to introduce an exception specificaion of a
function declaration, in which case, it isn't an operator, or it
can be used in an expression, in which case, it is. Except, of
course, that this distinctio between operators and other tokens
is rather superficial and meaningless. The only significant
distinction made by the standard is between pre-processor
op-or-punc (which includes new and delete, but not throw and
return), and the actual language tokens. The only real
difference between new and throw (or new and while) is that:

#define new 0 // Illegal, always...
#define throw 0 // Legal, unless you include a
// standard header.

For the rest, after preprocessing, both are tokens, and both are
keywords. There is no distiction between operators and
non-operators, at least in the standard. In common parlance, I
think that you'd consider something an operator if it is used in
an expression, and "punctuatio n" otherwise. In which case,
"throw" is like a comma: sometimes one, sometimes the other.
But then, so is int: it can introduce a declaration, but it can
also be used as a conversion operator.

In sum, the only real answer is that there is no such
distinction operator/non-operator, or that any such distinction
is arbitrary, and decided upon by the author.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 4 '07 #7
On Oct 4, 8:05 am, Kai-Uwe Bux <jkherci...@gmx .netwrote:
Ranganath wrote:
On Oct 3, 8:46 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
Ranganath wrote:
Why is throw keyword considered as an operator?
It is impossible to answer a question based on a wrong premise.
'throw' is not an operator. Why do you think it is one? Or,
rather, what do you mean "considered as an operator"? What is
the context?
throw is an operator.
According to the language standard, "throw" is a keyword, not an operator.
As far as I can see, the language standard doesn't define
operators. Without a definition, I'm incapable of saying
whether throw is an operator or not.
Please have a look at clauses [2.11] (listing all keywords) and [2.12]
(listing all operators and punctuators).
Please read the first sentence of §2.12:

The lexical representation of C++ programs includes a number
of preprocessing tokens which are used in the syntax of the
preprocessor or are converted into tokens for operators and
punctuators:"

Note the relevance with regards to the preprocessor. The fact
that throw isn't in this list means that:

#define throw catch

is legal (unless you include a standard header, in which case,
§17.4.3.1.1/2 applies: "A translation unit shall not #define or
#undef names lexically identical to keywords").

That is, in fact, all it means.

One may, of course, wonder about the conditions for inclusion in
this list. There's really no more reason for new to be in it,
than for throw. I suspect that it's more or less historical.
K&R (at least the original edition) had two lists, one for
operators, and one for punctuation (and the comma was in both).
I think Stroustrup originally followed this (although I don't
have my copy of his book handy to verify), adding new and delete
to the list of operators, because they can appear in an
expression. The C++ commitee dropped the distinction, adopting
rather the distinction pre-processor/parser, and merged the two
lists---logically, I suppose, new and delete should have been
dropped from the resulting list at that time. And the alternate
tokens got added---necessarily, since the pre-processor has to
recognize them. Throw got added to the language much later
(than new and delete), and since there was no need for the
preprocessor to know about it, it didn't get added to the list.

At any rate, there's no real logic behind it: the pre-processor
has no need to treat new and delete differently from throw, or
even while; on the other hand, it does treat the keywords true
and false differently (although they aren't in the list).
(IIRC, as late as CD2, there was no special treatment of true or
false by the pre-processor. Which meant that "#if true"
evaluated to "#if 0", and the following block would not be
compiled!)
Please have a look athttp://en.wikipedia.or g/wiki/Operators_in_C_ and_C++.
That would be a either a bug in wikipedia or may indicate that
wikipedia uses a different notion of "operator" (maybe
something that aims to be meaningful across programming
languages, but I am just guessing here).
All I wanted to know
is what constraint makes it to be treated as an operator.
Huh?
I think what he's really asking for is the definition of
operator. IMHO, there isn't really one, and asking whether
"throw" (or "while", or "new") are operators or not is a
meaningless question. (And the Wikipedia is a wonderful
resource for meaningless or unimportant questions---fun
questions, in other words---, even if it isn't very good as a
reference or for serious work.) I suspect that the definition
being used here is that an operator is a keyword or punctuation
that can be used in an expression (but then, what is the second
"int" in "int i = int(d);").

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 4 '07 #8
James Kanze wrote:
On Oct 3, 7:16 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
>Erik Wikström wrote:
>>On 2007-10-03 12:31, Ranganath wrote:
Why is throw keyword considered as an operator?
>>For the same reason that new, delete, and sizeof are operators I
would suspect. What else would it be, if it was not an operator?
>What's 'return'? 'throw' is exactly that.

Not really. Throw can be used in an expression, return can't.
>'delete' is an operator because you're allowed to overload it and
because the name of that function would be 'operator delete'. But
among the rest of true operators it stands out because it does not
yield a value and hence cannot be used in another expression.

You're forgetting the comma operator, and ?:. Things like:

MyClass::MyClas s( T arg )
: myMember( isValid( arg ) ? arg : throw "Invalid arg")
{
}

are perfectly valid and legal.
Yes, I did forget about those. I am guessing that's might be the
only reason to ever think of 'throw' as an operator. Of course, it
still does non yield any value and as such it's use in an expression
is rather special.
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 4 '07 #9
On Oct 4, 11:05 am, Kai-Uwe Bux <jkherci...@gmx .netwrote:
Ranganath wrote:
On Oct 3, 8:46 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
Ranganath wrote:
Why is throw keyword considered as an operator?
It is impossible to answer a question based on a wrong premise.
'throw' is not an operator. Why do you think it is one? Or,
rather, what do you mean "considered as an operator"? What is
the context?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
throw is an operator.

According to the language standard, "throw" is a keyword, not an operator.
Please have a look at clauses [2.11] (listing all keywords) and [2.12]
(listing all operators and punctuators).
Please have a look athttp://en.wikipedia.or g/wiki/Operators_in_C_ and_C++.

That would be a either a bug in wikipedia or may indicate that wikipedia
uses a different notion of "operator" (maybe something that aims to be
meaningful across programming languages, but I am just guessing here).
All I wanted to know
is what constraint makes it to be treated as an operator.

Huh?

Best

Kai-Uwe Bux
Hi,
Please have a look at "The C++ Programming Language" by Stroustrup 3rd
Edition, section 6.2.
It clearly mentions that throw is an operator in the table named
"Operator Summary".

- rr

Oct 4 '07 #10

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

Similar topics

3
1463
by: Gianni Mariani | last post by:
This is one of those, hugh ? moments. So, GCC behaves just like I would kinda expect it to but it looks VERY strange. It's one of those things that could cause silent strife if you included files in the wrong order. class X;
8
1756
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 has a size of 300000+ lines of code it is not so easy to do all of that manually. therefore my question: is there a tool that you run on your sources and which just converts the throw declarations into a comment? a tool that only comments the...
3
3490
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? Is this because there could be an exception thrown when the code creates a std::exception? I would assume that is not the case. However, if I want to create a new exception class, derived from std::exception (say MyException) then how can I...
1
1479
by: Ralph Moritz | last post by:
Hi, I've written a class called ConfigParser, which can be used to parse config files. (Surprise!) I have overloaded the shift operators to mimic the std iostreams. My question is, should overloaded operators throw or not? Both are non-member friends of ConfigParser. Cheers, Ralph
5
478
by: siddhu | last post by:
What happens when deletion falis?Does operator delete function throw exception? I assume that nothrow is not available with delete. Or is it available?
28
2839
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't apply. Can somebody tell me why? Thanks, Jess
2
1384
by: MikeP | last post by:
Hi there, Can simply assigning a (valid) value to a fundamental .NET type (int, bool, etc,. or even a reference) cause an exception in theory. I imagine it's theoretically possible (not sure what the standard says) but so unlikely that one need not worry about it in practice (assuming your app doesn't deal with life and death issues). I'm really more curious abut the issue. Thanks.
3
1696
by: Daniel T. | last post by:
This is basically what I have now... class Foo { /* definition irrelevant */ }; istream& operator>>( istream& is, Foo& foo ); // could throw int main() { ifstream file( "file.txt" ); Foo foo; int i = 0;
6
1955
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 <iostream> #include <string> using namespace std;
0
8427
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8850
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8523
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7355
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6178
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2749
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.