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
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.
| ||||||||||||||
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 As for what influences the memory: use of exceptions may require the The general rule is use exceptions to report exceptional circumstances, A design example: a function written "bool is_odd(int)" probably Cheers, Tony [ See http://www.gotw.ca/resources/clcm.htm for info about ] 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.
| ||||||||||||||
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 >You can also work out whether E is on the stack or not, but I made some tests. I create a recusive function with try/catch and log > 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). 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, It's an excelent topic! :) What is one exception condition? > not oft-encountered error conditions. Varying from this rule isn't > worth doing unless you have profiling results showing you that you have > to. I instead use exceptions for erros and for precoditions broken. for example: struct X{ }; 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! :) Thank's [ See http://www.gotw.ca/resources/clcm.htm for info about ] 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.
| ||||||||||||||
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, Broken preconditions should almost always be handled with an assert >> 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. 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. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] 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.
| ||||||||||||||
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 In much cases asserts work as comentary only; > 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. for example: void f(pointer *p){ } With exceptions the code will be response the error. The TC++PL has an example: template <classX , classA > inline void Assert(A assertion) { } 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? } 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 :) } 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! [ See http://www.gotw.ca/resources/clcm.htm for info about ] 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.
| ||||||||||||||
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 No, they work to stop program execution at the earliest point of >> 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; > void f(pointer *p){ 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) { Stop execution so I can debug the program. Good! > if (!assertion ) throw X(); > } > If think that the most useful is: > 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) { I don't understand the question. > // 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. > 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: Why not? Because, I presume, passing 0 is a precondition violation. > void func(vector &v, int *p) { > if (p == 0) > throw bad_arg(); // why not? because this functions alread throws 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? > } Even if it were not nothrow, it would have only one contract. > void func(vector &v, int *p) throw() { > // yes I can use reference but is only an example :) Yes, it's a common FUD. It's easy to do some experiments to get a > *p = v.size() + 10; > } > I think that is a long and important topic. feeling for the real numbers. Do they know the cost in correctness and maintainability of using -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] 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.
| ||||||||||||||
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: > 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 First, the reason why one would like to 'throw' in this case, which is Why that is a problem in C++: the standard exception hierarchy is not So, if you want a really "hard" exception in C++, one that is likely to And even that non-standard exception might be caught (and not rethrown) Well, then, why not use std::terminate instead? After all, that's what But no, that's not what it's for. Calling std::terminate does not ¹) One might argue that calling std::terminate is the only reasonable -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] 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.
| ||||||||||||||
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 [ See http://www.gotw.ca/resources/clcm.htm for info about ] 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.
| ||||||||||||||
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, 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 Happily it's not the case that every ATM machine that encounters a > 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). failed assertion has to be discarded. :-) Cheers, - Alf -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] 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.
| ||||||||||||||
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 > then the above is a sort of extremist version of my position. I would Normal execution doesn't involve killing the host process, even if it's > 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. 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, No, cleaning up open files after a process dies is not recovery, and it's > 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. 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 ] 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 |