Mon, 23 Mar 2009 09:07:16 -0400, bearophile wrote:

> grauzone:
> 
>>From your site:<
> 
> I don't own LiveJournal :-) That's just my blog, my site is elsewhere.
> 
>> Using exceptions in a string->int conversion routine is really horrible and 
>> incredibly stupid.<
> 
> I agree that it's not nice looking, but in Python that's the standard idiom.
> In D I do the same thing when I want to know if a string contains an integer 
> or float, with toInt/toFloat, how can I do it with no exceptions?
> 
> Python3 also removes the find() method of strings, and leaves only the 
> index() method, that is like find(), but raise ValueError when the substring 
> is not found. So you are forced to use exceptions here too.
> 
> So far in D I have used exceptions to control flow only once, in this library 
> module (original code idea by Witold Baryluk, modified):
> http://www.fantascienza.net/leonardo/so/dlibs/generators.html

D is designed to make normal execution flow fast and allow error
handling be not-so-fast:

http://www.digitalmars.com/d/2.0/errors.html

so Python idioms simply do not fit.  Different languages require
different idioms, that's my strong opinion.  One really important
benchmark would be something like this:

*********************
*********************
D exceptions

-----main.d-----------------
import worker;
void main() {
  for (int i = 0;; i++) {
    try {
      do_stuff(i);
    } catch (TerminateException e) {
      break;
    }
  }
}
------worker.d----------------
class TerminateException {}
void do_stuff(int i)
{
  if (i == 1_000_000_000)
    throw new TerminateException;
}
-----------------------

*******************
*******************
C++ exceptions

------main.cpp--------------------
#include "worker.h"
int main() {
  for (int i = 0;; i++) {
    try {
      do_stuff(i);
    } catch (const TerminateException & e) {
      break;
    }
  }
  return 0;
}
------worker.h--------------------
struct TerminateException {};
void do_stuff(int i);
------worker.cpp--------------------
#include "worker.h"
void do_stuff(int i)
{
  if (i == 1000000000)
    throw TerminateException();
}
--------------------------

***************
***************
results

D: 5.52s
C++: 4.96s

Reply via email to