3

Coming from a C++ background where I learned to restrict the use of exceptions to exceptional circumstances, I was very surprised about Python's cheap exceptions. In fact, some language features like iteration seem to be build on exceptions being thrown. This makes sense when considering that the (python) interpreter has to guarantee its own integrity and thus does several checks anyway.

Does it make sense to expect cheap exceptions to be normal in interpreted languages?

Edit:

To make clear that this question doesn't address primarily performance, I should add that I try to expand my programming skills by learning new languages. I'd probably never use exceptions in non-exceptional circumstances in C++ or Pascal, but in python I obviously should (and this is maybe not the only language). And because it makes a structural difference for my code if I use or not use exceptions, a simple guideline/rule of thumb would help to get started in a new language..

Community
  • 1
  • 1
Wolf
  • 9,679
  • 7
  • 62
  • 108
  • I'm not sure one should ever "expect" anything... Always refer to documentation and profiling when it comes to performance measures. – Matt Coubrough Dec 07 '14 at 20:14
  • @MattCoubrough It's not primary about performance, but the style of approaching a problem. – Wolf Dec 07 '14 at 20:15
  • I guess my comment was trying to say *"it can be dangerous to project assumptions about programming paradigms from one language to another"* even the concept of "interpretted" can vary between languages (ie JIT etc) – Matt Coubrough Dec 07 '14 at 20:30
  • Not really. In ruby, exceptions (`raise/rescue`) are one to two orders of magnitude slower than other control flow ( `break` or `catch/throw`). http://robotlibrarian.billdueber.com/2011/05/a-short-ruby-diversion-cost-of-flow-control-under-ruby/ – roippi Dec 07 '14 at 20:31
  • @MattCoubrough interesting point, I was afraid of that: [There is no such thing as a “compiled language” or “interpreted language”](http://stackoverflow.com/q/3440297/2932052) – Wolf Dec 07 '14 at 20:35

1 Answers1

3

See that in perspective:

In C++ all is cheap except exceptions;

In Python exceptions are about as time consuming as normal stuff.

Klamer Schutte
  • 1,063
  • 9
  • 18
  • 1
    Sure (+1). In C++ exception handling itself is also (or can be) cheap if no exception is thrown, but throwing should be considered expansive. – Wolf Dec 07 '14 at 20:31