Re: How expensive are exceptions?
On Jun 1, 12:53 pm, Roy Smith <r...@panix.com> wrote:
The conventional wisdom is that exceptions in C++ are expensive, but I've
never seen anything that say *how* expensive they are. I come from a
background using languages where exceptions are not particularly expensive,
and thus tend to use them in paces where perhaps I shouldn't (at least if
you believe the grief my co-workers give me).
You really have to measure against how you would handle control flow
and debugging without exceptions. As almost always, programmers who
cannot throw exceptions invariably start placing "isValid()" member in
their classes, and use return codes like C does. If you have a fairly
complex application, then the return codes quickly starting become
more the problem than the solution, not to mention the myriad objects
that can't possibly maintain their own class invariants. Out of
desperation, the developers start tracing everything to a file, hoping
to find the problem.
And THAT is far far worse than tens of thousands of instructions.
I wrote up some drafts (that I have yet to take out some of my
companies jargon for bugs (BF's and WP's) tha tillustrates this.
http://www.lancediduck.com/papers/Cpp/IndustrialStrengthCode1.htm
,http://www.lancediduck.com/papers/Cpp/IndustrialStrengthCode2.htm
To specifically answer the question on how expensive they are, that
depends on the compiler. Most compiliers implement in such a way that
entering a try block is cheap, and throwing is expensive. To compare
to Java, one would have to use finally blocks to reclaim every
resource that was acquired from the start of the try. This includes
memory. This is a feature of C++ RAII BTW, and occurs when you exit
the scope of ANY block, try or not. Reclaiming resources as part of
the applciation control flow (as opposed to deferring to a background
GC) can make it look like exceptions are expensive. What is really
expensive is freeing all the resources acquired.
In any case, dont do this
try{//bad way to do a for loop
int i=0;
while(true){
do_something();
if i++>100 throw 1;
}
}
}catch(...){}
Do do this
class Sumlessthan100{
unsigned x,y;
public:
Sumlessthan100(unsigned a, unsigned b):x(a),y(b){
if(!(x+y<100)||x>99 ||y>99){
std::ostringstream s;
s<<__FILE__<<":"<<__LINE__<<
"Sumlessthan100("<<x<<","<<y<<") sum must be less than
100";
throw std::logic_error(s.str());
}
}
Sumlessthan100(Sumlessthan100 const&r):x(r.x),y(r.y){
//does not throw, since r is guaranteed to be valid
}
unsigned getX()const;
unsigned getY()const;
void setX(unsigned newx){
Sumlessthan100 tmp(newx,y);
x=newx;
}
};
and with a small army of classes like this then you do
int processrequest(){//some very high level function
try{
unsigned myx=getInput(); //may throw
unsigned myy=getInputagain();//may throw
Sumlessthan100 cleaninput(myx,myy);//may throw
//at this point we enter the "no-fail zone"
processsideeffect1(cleaninput); //does not throw
processsideeffect2(cleaninput); //does not throw
}catch(std::exception const&e){
cout<<e.what();
return -1;
}catch(...){
cout<<"Unknown error";
return -1;
}
return 0;
}
Now imagine a program with dozens of inputs and hundres of validation
-- doing it without exceptions would be very painful. Exception may be
excessive overhead for very small programs, but scaling up without
them is masochistic.
Lance
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]