Hi all,
Just wondering whether there's any reason why exception specifications are
enforced at runtime, rather than at compile-time like in Java? (This was
prompted by reading an article on GOTW, incidentally.) Is there something
about C++ that makes it harder/impossible to check at compile-time?
Cheers,
Stu 41 3020
Stuart Golodetz wrote:
Just wondering whether there's any reason why exception specifications are
enforced at runtime, rather than at compile-time like in Java?
Beacause is a loose of time. I've seen plenty of Java code samples full of:
try something; catch anything required by the specification: Do nothing;
--
Salu2
"Julián Albo" <JU********@ter ra.eswrote in message
news:45******** @x-privat.org...
Stuart Golodetz wrote:
>Just wondering whether there's any reason why exception specifications are enforced at runtime, rather than at compile-time like in Java?
Beacause is a loose of time. I've seen plenty of Java code samples full
of:
try something; catch anything required by the specification: Do nothing;
--
Salu2
Whether exception specifications are a good thing or not is certainly up for
discussion :) What I don't understand though is what purpose is served by
enforcing them at runtime. If you're going to enforce them, surely it should
be at compile-time? I was just wondering if there's some technical
difficulty making that impossible, or at any rate very hard?
Cheers,
Stu
Stuart Golodetz skrev:
"Julián Albo" <JU********@ter ra.eswrote in message
news:45******** @x-privat.org...
Stuart Golodetz wrote:
Just wondering whether there's any reason why exception specifications
are
enforced at runtime, rather than at compile-time like in Java?
Beacause is a loose of time. I've seen plenty of Java code samples full
of:
try something; catch anything required by the specification: Do nothing;
--
Salu2
Whether exception specifications are a good thing or not is certainly up for
discussion :) What I don't understand though is what purpose is served by
enforcing them at runtime. If you're going to enforce them, surely it should
be at compile-time? I was just wondering if there's some technical
difficulty making that impossible, or at any rate very hard?
I believe it is hard. Consider:
std::vector<int v;
.....
int i = v.size() 0? v.at(0):0;
Should that statement be assumed to throw or not?
While it is easy for us to see, it is more difficult for a compiler.
And it can be made even more difficult if v.at(0) is replaced by a
function-call with v as a parameter. I believe you can see the point?
/Peter
Cheers,
Stu
Stuart Golodetz wrote:
>try something; catch anything required by the specification: Do nothing;
Whether exception specifications are a good thing or not is certainly up
for discussion :) What I don't understand though is what purpose is served
by enforcing them at runtime. If you're going to enforce them, surely it
should be at compile-time?
As shown by my sample, checking it at compile time does not enforce
anything. When people see a message from the compiler, immediately add code
like this. Or instead of doing nothing they abort the program, thus
effectively doing the check at runtime but having to write code to do it.
Surely C++ compilers can add a check and issue a warning when explicit
violations of the specification are detected, the reason they not do it is
that almost anybody wants it.
I was just wondering if there's some technical difficulty making that
impossible, or at any rate very hard?
People habitually don't add features or options to a compiler, or any other
program of certain size and complexity, just because there is not
impossible or very hard to do it. They do it when the authors think, or a
bunch of users tell him, that it will be useful.
If you are talking about the language standard, not the features of concrete
compilers, I suggest you to read "The design and evolution of C++".
--
Salu2
peter koch wrote:
I believe it is hard. Consider:
std::vector<int v;
....
int i = v.size() 0? v.at(0):0;
Should that statement be assumed to throw or not?
By the point of view of checking specifications is easy: the statement can
throw if and only if vector::size or vector::at can throw.
And it can be made even more difficult if v.at(0) is replaced by a
function-call with v as a parameter. I believe you can see the point?
In that case, the specification of that function is checked, same difficult.
--
Salu2
"Stuart Golodetz" <sg*******@dNiO aSl.PpAiMpPeLxE .AcSoEmwrites:
Whether exception specifications are a good thing or not is certainly up for
discussion :) What I don't understand though is what purpose is served by
enforcing them at runtime. If you're going to enforce them, surely it should
be at compile-time? I was just wondering if there's some technical
difficulty making that impossible, or at any rate very hard?
I disagree with the other answers to your question - I find exception
specifications almost useless for that reason; and in practice, indeed
most people don't use them. To quote Herb Sutter:
'In brief, don't bother with exception specifications. Even experts
don't bother.'
And the boost coding guidelines discourage using them either.
There are some minor uses for them that have to do with optimisations
a compiler can do when he knows that exceptions can't occur, but they
are really unusable for what they are for in language like java: To
document what functions throw and enforce this documenation.
If someone know why exception specifications are as brain-dead as they
are in C++, I'd be intruiged to hear. My guess is some compatibility
rationale.
Jens
Julián Albo wrote:
peter koch wrote:
>I believe it is hard. Consider:
std::vector<in tv; ....
int i = v.size() 0? v.at(0):0; Should that statement be assumed to throw or not?
By the point of view of checking specifications is easy: the
statement can throw if and only if vector::size or vector::at can
throw.
But vector::size doesn't throw, and vector::at isn't called if the
condition is such that it could throw. So is this statement throwing
or not? Can the compiler detects that?
>
>And it can be made even more difficult if v.at(0) is replaced by a function-call with v as a parameter. I believe you can see the point?
In that case, the specification of that function is checked, same
difficult.
The other problem is with templates, where the throw spec really
depends on the template parameter
template<class T>
void do_something(T x); // might throw for some Ts
How do we express that?
Bo Persson
Bo Persson wrote:
>>int i = v.size() 0? v.at(0):0; Should that statement be assumed to throw or not?
By the point of view of checking specifications is easy: the statement can throw if and only if vector::size or vector::at can throw.
But vector::size doesn't throw, and vector::at isn't called if the
condition is such that it could throw. So is this statement throwing
or not? Can the compiler detects that?
What compiler? Maybe a compiler that does it can be written, at least for
standard functions with well defined behavior, but I don't think there is a
great demand for such feature. By the way, in this example you can drop
'at' and use operator [ ] instead.
The other problem is with templates, where the throw spec really
depends on the template parameter
template<class T>
void do_something(T x); // might throw for some Ts
How do we express that?
I suppose that a compiler that check the specifications at compile time
'java style' will check at complete specialization of the template time.
Supposing that such compiler will exist any time. One can imagine a
sintaxis to express something as: "can throw what T::somefunc throws", but
I suspect that such complications will never be used by any programmer.
--
Salu2
In article <AL************ ********@pipex. net>, sg*******@dNiOa Sl.PpAiMpPeLxE. AcSoEm says...
Hi all,
Just wondering whether there's any reason why exception specifications are
enforced at runtime, rather than at compile-time like in Java? (This was
prompted by reading an article on GOTW, incidentally.) Is there something
about C++ that makes it harder/impossible to check at compile-time?
The following is based primarily on recollection, so it may need to be
taken with a grain of salt.
In a phrase, backward compatibility. Exception specifications were added
to C++ after there were a number of implementations of exception
handling. These were consistent enough that it was considered reasoanble
and important to maintain compatibility with them. To maintain
compatibility with this code, the rule was made that lack of an
exception specification meant "can throw anything."
Unfortunately, that also meant that essentially all existing code was
seen (by the compiler) as being able to throw anything -- even pure C
code that had no notion of exception handling at all.
If exception specifications were enforced at compile time, new code that
used them would have had to do one of two things: either rewrite all
existing code to include exception speciifications (incidentally,
breaking all C compatibility) or else include a 'catch(...)' clause in
the new code to catch and convert the (probably nonexistent) "other"
exceptions to something it was allowed to throw.
Neither of these was seen as acceptable, leaving run-time enforcement as
nearly the only possible solution.
--
Later,
Jerry.
The universe is a figment of its own imagination. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Philipp Holzschneider |
last post by:
Are there any available c++ compilers around that strictly
implement the mentioned "Exception Specification"
feature??
where the following compiles
void func() { throw A(); };
void gunc() throw(A,B) { throw A(); };
----
void thrower() throw(A) { throw A(); };
|
by: Scott Brady Drummonds |
last post by:
Hi, all,
I just got out of a meeting with a team of software developers that I
recently joined as they are staffing to create a medium-sized project
(potentially all of which will be written in C++). From my experience and
readings, I've come to this group indoctrinated to use exceptions for error
handling. The experience in this group...
|
by: linq936 |
last post by:
Hi,
I have many assert() call in my code, now I am considering to replace
them with exception. The reason I want to do this change is that with
the program going bigger and bigger, it is hard to test all the corner
cases, and thus assert() does not work as good as before. The problem
is if there are something really bad which was not captured...
|
by: benben |
last post by:
Why doesn't the following code work?
template <typename ExceptionT>
void f(void) throw (ExceptionT)
{
throw ExceptionT();
}
Ben
|
by: Brian |
last post by:
I'm trying to comile some managed C++ code in VC++ 2005 beta that has a lot
of exception specifications,ie
int func() throw( SomeException*) ;
I'm getting these messages:
error C2353: exception specification is not allowed.
unfortunately MSDN isn't much help here. Obviously, C++/CLI is not allowing
| |
by: junw2000 |
last post by:
Is C++ Exception handling useful? think it is too complicated. What
kinds of project need to use it? Thanks.
|
by: George2 |
last post by:
Hello everyone,
How do you understand the Bjarne's comments about exception
specification? Especially, "not required to be checked across
compilation-unit" and "violations will not be caught at run time"?
section 14.6.1 Checking Exception Specifications
--------------------
|
by: Ioannis Vranos |
last post by:
Perhaps a mechanism can be introduced in the C++0x/1x standard,
something simple like defining a function as:
void somefunc(void) throw()
{
// ...
}
|
by: Keith Halligan |
last post by:
I'm a bit unsure about exception specifications and the rules that
they enforce.
I've had a look at the C++ spec and it doesn't state about the errors
that it enforces, if an exception is thrown in a function that isn't
specified in its exception specification (assuming there's one
there).
If I compile the code below on Solaris, Linux...
|
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...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
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. ...
|
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...
|
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...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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...
|
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...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |