try-catch vs. the others in error handling

try-catch vs. the others in error handling

Post by Kim, Seungta » Sat, 02 Aug 2003 03:02:29



We discussed about the try-catch vs. the others in
error handling in side of speed in microsoft.public.vc.language.
But, there is MS specific discussion only discussed.

I wonder how you think about the next subject.

1. the try-catch mechanism is too slow(at least in MSVS). why?
(In my environments, 700~1000 times slower)

It depend on Implementation defined problem?
    - If so, how about the result in your environments?
    - If not, what is the point of slow the in the "try-catch"?

##Sample seudo code

##1. case of error returing and check

    ERR f(){

        if( case err ) return error;
        ...
    }

    ERR e = f();
    if(e=err) ...

##2. case of try-catch

    void f() throw(ERR){

        if( case err ) throw err;
    }

    try{
        f();
    }
    catch(ERR){...}

2. If the try-catch mechanism is slow(regardless of
implementation defined or not), the try-catch mechanism
is useless in speed inportant applications?

In normal case, I think that the try-catch mechanism
needs not to make any instance(temp variables for
returning and checking) and to check the results.
I think that it's still worth part of try-catch stratgy in
speed important applications.

Thanks in advices.


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

 
 
 

try-catch vs. the others in error handling

Post by Dhru » Sat, 02 Aug 2003 17:25:35


 >
 > We discussed about the try-catch vs. the others in
 > error handling in side of speed in microsoft.public.vc.language.
 > But, there is MS specific discussion only discussed.
 >
 > I wonder how you think about the next subject.
 >

[snip a lot of code]......

 > 2. If the try-catch mechanism is slow(regardless of
 > implementation defined or not), the try-catch mechanism
 > is useless in speed inportant applications?
 >
 > In normal case, I think that the try-catch mechanism
 > needs not to make any instance(temp variables for
 > returning and checking) and to check the results.
 > I think that it's still worth part of try-catch stratgy in
 > speed important applications.
 >

I'm no compiler implementation expert, but I think that exceptions were
in*ded mainly to make code more readable, and maintainable, and all
that stuff that you don't see in production code :-) (For more info on
that, see D&E, by Mr. Bjarne Stroustrup). Imagine reading or even writing
code with error checks in every line, and at every function call.

{A word of advice to posters, omit honorifics (such as Mr, Dr, Ms etc.)
as that avoids the potential embarrassment of getting them wrong. I am

irritated to be referred to as Ms Francis Glassborow -- as sometimes
happens in emails. -mod}

Theoritically, I guess unless an
exception is thrown, C++ code should be as fast or faster than C code.
Because there is no checking for the return value, and stuff......
Only when the exception is thrown, it becomes slow......

Regards,
-Dhruv.

      [ See http://www.veryComputer.com/]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

 
 
 

try-catch vs. the others in error handling

Post by Ben Hutching » Sat, 02 Aug 2003 17:28:59


 >
 > We discussed about the try-catch vs. the others in
 > error handling in side of speed in microsoft.public.vc.language.
 > But, there is MS specific discussion only discussed.
 >
 > I wonder how you think about the next subject.
 >
 > 1. the try-catch mechanism is too slow(at least in MSVS). why?
 > (In my environments, 700~1000 times slower)

Slower than returning and checking error codes?  Yes, it's slower
when an error does occur, but it's faster the rest of the time.

 > It depend on Implementation defined problem?
 >     - If so, how about the result in your environments?

Microsoft's implementation of exceptions is particularly slow.

 >     - If not, what is the point of slow the in the "try-catch"?

It is slow because it requires a run-time search for an exception
handler, possibly involving dynamic type checking and conversion.

<snip>
 > 2. If the try-catch mechanism is slow(regardless of
 > implementation defined or not), the try-catch mechanism
 > is useless in speed inportant applications?
 >
 > In normal case, I think that the try-catch mechanism
 > needs not to make any instance(temp variables for
 > returning and checking) and to check the results.
 > I think that it's still worth part of try-catch stratgy in
 > speed important applications.

Right.  Exceptions let you speed up the usual case at the expense
of slow handling of errors (or other unusual cases).  For most
applications this is a good trade-off.

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

 
 
 

try-catch vs. the others in error handling

Post by John Tor » Sun, 03 Aug 2003 13:32:59


Quote:> ##2. case of try-catch

>     void f() throw(ERR){

>         if( case err ) throw err;
>     }

>     try{
>         f();
>     }
>     catch(ERR){...}

- usually you throw an object of a class type.
- also, avoid the exception specification, since that will make the function slower
(instead of 'void f() throwS(ERR)', just use void f())

- I can't believe it's 700-1000 times slower, since noone would use it.
  I think you built the tests wrong.
  Could you show the real code?

Best,
John

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

 
 
 

try-catch vs. the others in error handling

Post by David Abraham » Sun, 03 Aug 2003 17:50:49


 >  > It depend on Implementation defined problem?
 >  >     - If so, how about the result in your environments?
 >
 > Microsoft's implementation of exceptions is particularly slow.
 >
 >  >     - If not, what is the point of slow the in the "try-catch"?
 >
 > It is slow because it requires a run-time search for an exception
 > handler, possibly involving dynamic type checking and conversion.

It's also slow for the non-exceptional case (unless you're on
IA64, where they have the same zero-overhead model as GCC), because
every potential cleanup requires the registration of an exception
frame at runtime.

--
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! ]

 
 
 

try-catch vs. the others in error handling

Post by Kim, Seungta » Mon, 04 Aug 2003 07:13:59



> [...]
> - also, avoid the exception specification, since that will make the function slower
> (instead of 'void f() throwS(ERR)', just use void f())

But, the effect is so small, not just like a "try-catch".

And the exception spec. help clients use the facilities more easily.
How do you know the kinds of exception when you use
a function if there is no exception-spec? I think that it's not
an option, but an essentiality.

Quote:> - I can't believe it's 700-1000 times slower, since noone would use it.
>   I think you built the tests wrong.

Not only me.

How about your result? I wonder what the result is in other
varing environments.

Quote:>   Could you show the real code?

Bellow I present. It just simple.

I make experiment on two function for measurement of try-catch's
performance. First one throws an exception
and another returns a value. In try-catch strategy, catch the
exception and do just nothing, and loop 100000 times it. In returning
strategy, receive value, check for kinds of error, and
loop 100000 times it, also.

Note. This experiment is not suitable in normal error-handling case.
Only for comparision of two strategy's speed performance.

Regards.


-- Sample Program : modified from Jochen Kalmbach's one --

// Only for MSVS
// Tested in MSVS6.0 and Win2K
//
// My Results :
//    try-catch: 695.589ms
//    result-code: 0.719365ms
//

#include <iostream>
#include <windows.h>

// check time
class CExcatTimeDiff
{
public:
 CExcatTimeDiff()
 {
  QueryPerformanceCounter((LARGE_INTEGER*) &m_BeginTime);
  m_EndTime = 0;
  m_Frequency = 0;
 }
 void Begin()
 {
  QueryPerformanceCounter((LARGE_INTEGER*) &m_BeginTime);
 }
 void End()
 {
  QueryPerformanceCounter((LARGE_INTEGER*) &m_EndTime);
 }
 double GetDiffInMs()
 {
  if (m_Frequency == 0)
   QueryPerformanceFrequency((LARGE_INTEGER*) &m_Frequency);
  if (m_EndTime == 0)
   End();
  return ((double)m_EndTime - (double)m_BeginTime) / ((double)m_Frequency
   / 1000);
 }
private:
 __int64 m_BeginTime;
 __int64 m_EndTime;
 __int64 m_Frequency;

Quote:};

// #####################################################

void ThrowFunction() throw(int)
{
 throw int(); // means err

Quote:}

int ResultFunction()
{
 return 1; // means err

Quote:}

int _tmain(int argc, _TCHAR* argv[])
{
 volatile int i;
 CExcatTimeDiff timerThrow;
 CExcatTimeDiff timerResult;

 // try-catch
 timerThrow.Begin();
 for(i=0;i<100000; i++)
 {
  try
  {
   ThrowFunction();
  }
  catch(int){}
 }
 timerThrow.End();

 // result-codes
 timerResult.Begin();
 volatile int res;
 for(i=0;i<100000; i++)
 {
  res = ResultFunction();
  switch(res)
  {
  case 0: // normal
   break;
  case 1: // err
   break;
  }
 }
 timerResult.End();

 std::cout<<"try-catch: "<<timerThrow.GetDiffInMs() << "ms"<< std::endl;
 std::cout<<"result-code: "<<timerResult.GetDiffInMs() << "ms"<< std::endl;

 return 0;

Quote:}

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

try-catch vs. the others in error handling

Post by Sebastian Molesk » Mon, 04 Aug 2003 18:10:52




 > > [...]
 > > - also, avoid the exception specification, since that will make the function
slower
 > > (instead of 'void f() throwS(ERR)', just use void f())
 >
 > But, the effect is so small, not just like a "try-catch".
 >
 > And the exception spec. help clients use the facilities more easily.
 > How do you know the kinds of exception when you use
 > a function if there is no exception-spec? I think that it's not
 > an option, but an essentiality.

Non-empty exception specifications, if implemented correctly, are as slow as a
"try-catch". The following pieces of code are equivalent:

// (1)
void foo() throw (MyException) {
     // my code

Quote:}

// (2)
void foo() {
     try {
         // my code
     } catch (MyException) {
         throw;
     } catch (...) {
         unexpected();
     }

Quote:}

Also, exception specifications become a problem if you apply them to virtual
functions as well as function pointers. If that's acceptable to you, don't worry
about using exception specifications. Otherwise I would suggest you stay away
from them.

sm

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

 
 
 

1. try-catch: How is an exception w/ multiple catches processed?

When an exception is thrown, w/ multiple catch blocks how is it
processed?

The example I had was

try {
        throw CMemException;

catch (CException) {            // Base class of CMemException
        cout<<"CException"<<endl

catch (CMemException ) {
        cout<<"CMemException"<<endl

What would the output be?  Do both catch statements get processed, just
the
first one, or just the more specific one.  Thanks.


      [ about comp.lang.c++.moderated. First time posters: do this! ]

2. How to get "submit" button to work

3. COM Add-in vs. Automation vs. Word Add-ins vs...

4. ssh-client connects to openssh-server

5. Good URL for Expection Handling and Try\Catch Explainations.

6. Win98 login

7. DLL call only works in try/catch block, but catch block is never called

8. Basic question - do drivers have to be installed on all comps on the network

9. try/catch does not catch SIGSEGV

10. Overhead of try-catch blocks?

11. DDE vs OLE vs Others?

12. Er, handling errors in an error handling routine?