Exceptions
flag
Messages 1 - 10 of 99 - Collapse all
/groups/adfetch?adid=i-ZvWRIAAAB-HXaWmyM-gWlqxK8rs-agHOOf3QyR6bPzxOY7iJ1W1A
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
1.  Thiago R. Adams  
View profile  
 More options Jun 29 2005, 10:50 am
Newsgroups: comp.lang.c++.moderated
From: "Thiago R. Adams" <thiago.ad...@gmail.com>
Date: 29 Jun 2005 10:50:59 -0400
Local: Wed, Jun 29 2005 10:50 am
Subject: Exceptions
Hi,
I have a lot of questions about exceptions.

In the sample below:

struct E(
 . . .
 ~E(){}
);

int main()
{
  try {
    throw E();
  }
  catch(const E &e) {
    e.some();
  }

}

When ~E will be called? Which is the E scope?

Where E will be created? In the Stack?

Exceptions are slow if are not throwed?

What makes exceptions slower or fast? The number of catch's influences?

Using exceptions, what influences the memory?

And what influences the program size?

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
2.  tony_in_da...@yahoo.co.uk  
View profile  
 More options Jun 30 2005, 7:09 am
Newsgroups: comp.lang.c++.moderated
From: tony_in_da...@yahoo.co.uk
Date: 30 Jun 2005 07:09:33 -0400
Local: Thurs, Jun 30 2005 7:09 am
Subject: Re: Exceptions
Firstly, I won't answer "when ~E will be called", because a basic
programming skill is putting a few cout statements in to find out for
yourself.  You can also work out whether E is on the stack or not, but
this is a bit more difficult - the answer is yes.  My recollection is
that E() will be created on the normal program stack, and the catch
statement may run in a dedicated exception stack (which allows
exceptions to work when the main stack has been exhausted due to
excessive recursion).

Anyway, entering and leaving a try { } block may have overheads in many
environments, but they can vary massively.  Again, a basic programming
skill is to benchmark things in your own environment, and/or look at
the assembly output from your compiler (e.g. g++ -S).  On Solaris a
couple years ago, I found reporting errors via exceptions was about 15
times slower than using integral return codes (like libc), but things
may well have changed.  I don't recall the overhead for successful
processing.  Anyway, whether this is significant depends on many
factors.  It just confirmed my impression that the programmer I had
just interviewed was being a bit silly when he insisted that he hadn't
reported an error using anything other than an exception for several
years.

As for what influences the memory: use of exceptions may require the
compiler to reserve an additional stack, but the standard C++ library
uses exceptions itself, so you should have that overhead anyway.  Re
program size, it's just not relevant unless you're in an embedded
environment, in which case I'd say again that you have to write code
and make measurements with your particular compiler and processor.

The general rule is use exceptions to report exceptional circumstances,
not oft-encountered error conditions.  Varying from this rule isn't
worth doing unless you have profiling results showing you that you have
to.

A design example: a function written "bool is_odd(int)" probably
shouldn't throw, but "void assert_odd(int)" could, because the caller
is clearly saying it would be exception not to succeed.  See
Stroustrup's TC++PL3 for some background discussions on use of
exceptions.

Cheers, Tony

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
3.  Thiago R. Adams  
View profile  
 More options Jun 30 2005, 1:30 pm
Newsgroups: comp.lang.c++.moderated
From: "Thiago R. Adams" <thiago.ad...@gmail.com>
Date: 30 Jun 2005 13:30:31 -0400
Local: Thurs, Jun 30 2005 1:30 pm
Subject: Re: Exceptions
Hi Tony,

My question is about destructor. When it be called? Yes I made some
tests with cout. I guess that the destructor is called at more external
try. And the compiler is smart if you use um rethrown. But this
behavior must be written is some place? or not?

>You can also work out whether E is on the stack or not, but
> this is a bit more difficult - the answer is yes.  My recollection is
> that E() will be created on the normal program stack, and the catch
> statement may run in a dedicated exception stack (which allows
> exceptions to work when the main stack has been exhausted due to
> excessive recursion).

I made some tests. I create a recusive function with try/catch and log
the address of stack variables. The stack address of variables doesn't
change with the exceptions. (I used visual c++ 2005 express)
Then I also guess that is in other stack.

> The general rule is use exceptions to report exceptional circumstances,
> not oft-encountered error conditions.  Varying from this rule isn't
> worth doing unless you have profiling results showing you that you have
> to.

It's an excelent topic! :) What is one exception condition?
I instead use exceptions for erros and for precoditions broken.
for example:

struct  X{
 X(AnyInterface *p) {
   if (p == 0)
    throw runtime_error("AnyInterface is obrigatory for class X!");
   else
     p->f();
 }

};

I need known the exceptions overhead and behavior to convince my
coworkers that is good.
Today we use a lot of return codes.

TC++PL3 is very good! :)
I read also:
CUJ august 2004, "When, for what, and how should you use exceptions"
Sutter
Exceptional C++ Style and C++ Coding Standard.

Thank's
http://paginas.terra.com.br/informatica/thiago_adams/eng/index.htm

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
4.  David Abrahams  
View profile  
 More options Jul 1 2005, 5:50 am
Newsgroups: comp.lang.c++.moderated
From: David Abrahams <d...@boost-consulting.com>
Date: 1 Jul 2005 05:50:27 -0400
Local: Fri, Jul 1 2005 5:50 am
Subject: Re: Exceptions
"Thiago R. Adams" <thiago.ad...@gmail.com> writes:

>> The general rule is use exceptions to report exceptional circumstances,
>> not oft-encountered error conditions.  Varying from this rule isn't
>> worth doing unless you have profiling results showing you that you have
>> to.
> It's an excelent topic! :) What is one exception condition?
> I instead use exceptions for erros and for precoditions broken.

Broken preconditions should almost always be handled with an assert
and not an exception.  An exception will usually cause a great deal of
code to be executed before you get a chance to diagnose the problem.

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
5.  Thiago R. Adams  
View profile  
 More options Jul 1 2005, 1:02 pm
Newsgroups: comp.lang.c++.moderated
From: "Thiago R. Adams" <thiago.ad...@gmail.com>
Date: 1 Jul 2005 13:02:33 -0400
Local: Fri, Jul 1 2005 1:02 pm
Subject: Re: Exceptions

> Broken preconditions should almost always be handled with an assert
> and not an exception.  An exception will usually cause a great deal of
> code to be executed before you get a chance to diagnose the problem.

In much cases asserts work as comentary only;
for example:

void f(pointer *p){
  assert(p != 0);
  p->f();

}

With exceptions the code will be response the error.

The TC++PL has an example:

template <classX , classA > inline void Assert(A assertion) {
  if (!assertion ) throw X();

}

If think that the most useful is:
template <classX , classA > inline void Assert(A assertion) {
  DebugBreak(); // stops debug
  if (!assertion ) throw X();

}

void f2(int* p) {
  // ARG_CHECK is an constant: true for checks args
  Assert<Bad_arg>(ARG_CHECK || p !=0 );

}

If code is correct the all callers will be test for arguments.
Then I can remove this test. (ARG_CHECK) But is nescessary?

When use throw and when use asserts?
Two cases:
void func(vector &v, int *p) {
  if (p == 0)
    throw bad_arg(); // why not? because this functions alread throws
  v.push_back(p); // push can throw right?

}

void func(vector &v, int *p) throw() {
 // maybe is better, becase this function no throw
 // it has only one contract with callers in debug and release
 assert(p != 0);

 // yes I can use reference but is only an example :)
 *p = v.size() + 10;

}

I think that is a long and important topic.
Performance in exceptions is important to decide the use.
My coworks says: "If we doesn't kwown the behavior and performance
penalities of exceptions, we will use returns codes, because returns
codes is more simple and is known"

Thanks!
(and sorry my english is not so good)

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
6.  David Abrahams  
View profile  
 More options Jul 1 2005, 10:04 pm
Newsgroups: comp.lang.c++.moderated
From: David Abrahams <d...@boost-consulting.com>
Date: 1 Jul 2005 22:04:21 -0400
Local: Fri, Jul 1 2005 10:04 pm
Subject: Re: Exceptions
"Thiago R. Adams" <thiago.ad...@gmail.com> writes:

>> Broken preconditions should almost always be handled with an assert
>> and not an exception.  An exception will usually cause a great deal of
>> code to be executed before you get a chance to diagnose the problem.

> In much cases asserts work as comentary only;
> for example:

> void f(pointer *p){
>   assert(p != 0);
>   p->f();
> }

No, they work to stop program execution at the earliest point of
detection of the error.

> With exceptions the code will be response the error.

If preconditions are broken, your program state is broken, by
definition.  Trying to recover is generally ill-advised.

> The TC++PL has an example:

That doesn't make it right :)

> template <classX , classA > inline void Assert(A assertion) {
>   if (!assertion ) throw X();
> }

> If think that the most useful is:
> template <classX , classA > inline void Assert(A assertion) {
>   DebugBreak(); // stops debug

Stop execution so I can debug the program.  Good!  

>   if (!assertion ) throw X();
> }

If the assertion fails when there is no debugger, how do you expect
the program to recover?

> void f2(int* p) {
>   // ARG_CHECK is an constant: true for checks args
>   Assert<Bad_arg>(ARG_CHECK || p !=0 );
> }

> If code is correct the all callers will be test for arguments.
> Then I can remove this test. (ARG_CHECK) But is nescessary?

I don't understand the question.

> When use throw and when use asserts?

Use asserts to detect that the invariants you have designed into your
program are broken.  Use throw to indicate that a function will not be
able to fulfill its usual postconditions, and when the immediate
caller is not very likely to be able to handle the error directly and
continue (otherwise, use error return codes and the like).

> Two cases:
> void func(vector &v, int *p) {
>   if (p == 0)
>     throw bad_arg(); // why not? because this functions alread throws

Why not?  Because, I presume, passing 0 is a precondition violation.
It depends on what you put in your documentation.  If you say, "you
must pass me a non-null pointer," then use an assert.  If you say, "if
you pass a null pointer I'll throw," well, then throw.  However, the
former is usually the better course of action.

>   v.push_back(p); // push can throw right?

Yes.  So what?

> }

> void func(vector &v, int *p) throw() {
>  // maybe is better, becase this function no throw
>  // it has only one contract with callers in debug and release
>  assert(p != 0);

Even if it were not nothrow, it would have only one contract.

>  // yes I can use reference but is only an example :)
>  *p = v.size() + 10;
> }

> I think that is a long and important topic.
> Performance in exceptions is important to decide the use.
> My coworks says: "If we doesn't kwown the behavior and performance
> penalities of exceptions, we will use returns codes, because returns
> codes is more simple and is known"

Yes, it's a common FUD.  It's easy to do some experiments to get a
feeling for the real numbers.

Do they know the cost in correctness and maintainability of using
return codes?

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
7.  Alf P. Steinbach  
View profile  
 More options Jul 2 2005, 6:23 am
Newsgroups: comp.lang.c++.moderated
From: al...@start.no (Alf P. Steinbach)
Date: 2 Jul 2005 06:23:03 -0400
Local: Sat, Jul 2 2005 6:23 am
Subject: Re: Exceptions
* David Abrahams -> Thiago R. Adams:

> > If think that the most useful is:
> > template <classX , classA > inline void Assert(A assertion) {
> >   DebugBreak(); // stops debug

> Stop execution so I can debug the program.  Good!

> >   if (!assertion ) throw X();
> > }

> If the assertion fails when there is no debugger, how do you expect
> the program to recover?

That's actually a good _C++_ question... ;-)

First, the reason why one would like to 'throw' in this case, which is
usually not to recover in the sense of continuing normal execution, but
to recover enough to do useful logging, reporting and graceful exit on
an end-user system with no debugger and other programmer's tools
(including, no programmer's level understanding of what goes on).

Why that is a problem in C++: the standard exception hierarchy is not
designed.  Uh, I meant, it's not designed with different degrees of
recoverability in mind.  At best you can use std::runtime_error for
"soft" (in principle recover-and-continue-normally'able) exceptions, and
classes derived otherwise from std::exception for "hard" exceptions, but
in practice people tend to not restrict themselves to
std::runtime_error, and the Boost library is an example of this common
practice  --  the standard's own exception classes are also examples.

So, if you want a really "hard" exception in C++, one that is likely to
propagate all the way up to the topmost control level, you'll have to
use something else than a standard exception.

And even that non-standard exception might be caught (and not rethrown)
by a catch(...) somewhere.  Which is not as unlikely as it might seem.
E.g., as I recall, ScopeGuard does that in its destructor¹.

Well, then, why not use std::terminate instead?  After all, that's what
it's for, isn't it?  And it's configurable.

But no, that's not what it's for.  Calling std::terminate does not
guarantee RAII cleanup as a "hard" exception would.  In short, I know of
no good portable solution to this problem in standard C++, and thinking
of how extremely easily it could have been supported in the design of
the standard exception classes (there was even existing practice from
earlier languages indicating how it should be) it's very frustrating.

¹) One might argue that calling std::terminate is the only reasonable
    failure handling in a destructor, even for ScopeGuard-like objects.
    But the standard already provides that draconian measure for the
    situation where it's really needed, where you would otherwise have a
    double exception (which does not exist in C++).  Doing it explicitly
    just removes a measure of control from the client code programmer.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
8.  Peter Dimov  
View profile  
 More options Jul 3 2005, 6:11 am
Newsgroups: comp.lang.c++.moderated
From: "Peter Dimov" <pdi...@gmail.com>
Date: 3 Jul 2005 06:11:16 -0400
Local: Sun, Jul 3 2005 6:11 am
Subject: Re: Exceptions

No recovery is possible after a failed assert. A failed assert means
that we no longer know what's going on. Generally logging and reporting
should be done at the earliest opportunity; if you attempt to "recover"
you may be terminated and no longer be able to log or report.

In some situations (ATM machine in the middle of a transaction, say) it
might make sense to attempt recovery even when an assertion fails, if
things can't possibly get any worse. This is extremely fragile, of
course. You can't test how well the recovery works because by
definition it is only executed in situations that your tests did not
cover (if they did, you'd have fixed the code to no longer assert).

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
9.  Alf P. Steinbach  
View profile  
 More options Jul 3 2005, 6:25 pm
Newsgroups: comp.lang.c++.moderated
From: al...@start.no (Alf P. Steinbach)
Date: 3 Jul 2005 18:25:19 -0400
Local: Sun, Jul 3 2005 6:25 pm
Subject: Re: Exceptions
* Peter Dimov:

If "recovery" in the above means continuing on with normal execution,
then the above is a sort of extremist version of my position.  I would
not go that far, especially in light of contrary facts.  For example,
one of the most used programs in the PC world, the Windows Explorer,
does continue normal execution in this situation simply by restarting
itself, so with this meaning of "recovery" the above is false.

If "recovery" in the above means something generally more reasonable,
like, cleaning up, then again that flies in the face of established
fact.  E.g., generally open files are closed by the OS whenever a
process terminates, whatever the cause of the termination, and it's no
big deal to define that as part of the application.  In this sense of
"recovery" the above therefore says it's impossible to write an OS in
C++, and that's patently false, too.

So what does the above mean, if anything?

Is it, perhaps, a "proof", from principles, that hummingbirds can't fly?

Then I'd suggest the proof technique and/or the principles are to blame.

> In some situations (ATM machine in the middle of a transaction, say) it
> might make sense to attempt recovery even when an assertion fails, if
> things can't possibly get any worse. This is extremely fragile, of
> course. You can't test how well the recovery works because by
> definition it is only executed in situations that your tests did not
> cover (if they did, you'd have fixed the code to no longer assert).

Happily it's not the case that every ATM machine that encounters a
failed assertion has to be discarded. :-)

Cheers,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
10.  Peter Dimov  
View profile  
 More options Jul 4 2005, 1:43 pm
Newsgroups: comp.lang.c++.moderated
From: "Peter Dimov" <pdi...@gmail.com>
Date: 4 Jul 2005 13:43:32 -0400
Local: Mon, Jul 4 2005 1:43 pm
Subject: Re: Exceptions

You said: "one would like to 'throw' in this case" ... "to recover enough to
do useful logging, reporting and graceful exit."

So I assumed that by "recovery" you mean "stack unwinding" (with its usual
side effects), because this is what 'throw' does.

> then the above is a sort of extremist version of my position.  I would
> not go that far, especially in light of contrary facts.  For example,
> one of the most used programs in the PC world, the Windows Explorer,
> does continue normal execution in this situation simply by restarting
> itself, so with this meaning of "recovery" the above is false.

Normal execution doesn't involve killing the host process, even if it's
automatically restarted after that. Exit+restart is recovery, for a
different definition of "recovery", but it's not performed by throwing an
exception.

> If "recovery" in the above means something generally more reasonable,
> like, cleaning up, then again that flies in the face of established
> fact.  E.g., generally open files are closed by the OS whenever a
> process terminates, whatever the cause of the termination, and it's no
> big deal to define that as part of the application.  In this sense of
> "recovery" the above therefore says it's impossible to write an OS in
> C++, and that's patently false, too.

No, cleaning up open files after a process dies is not recovery, and it's
not performed by throwing an exception.

> So what does the above mean, if anything?

It means that performing stack unwinding after a failed assert is usually a
bad idea.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google