loading...

Why do all the dynamic languages catch name errors by default?

yujiri8 profile image Ryan Westlund Updated on ・1 min read

For all my experience with the try-catch paradigm, I've never seen a situation where I wanted a name error in the try block to be caught. It's hard to imagine one. A name error is a typo 99% of the time, and a mistake 99% of the remainder. Catching it accidentally can lead to an especially frustrating debugging experience.

So why do all the dynamic languages have catch statements catch name errors by default?

Unlike Julia and Javascript, Python and Ruby have real exception hierarchy systems and allow multiple catch clauses that catch different kinds. But both of them still include name errors in a bare catch.

And their designers obviously realized that there are some exceptions you don't usually want to catch, as they have a class you can catch that's only the things you'd normally want to: StandardError in Ruby, Exception in Python (with BaseException being the real progenitor). But for some reason both of them consider NameError a subclass of this!

Why do they do all this? And are there dynamic languages that don't?

Posted on by:

yujiri8 profile

Ryan Westlund

@yujiri8

I'm a programmer, writer, and philosopher. My Github account is yujiri8; all my content besides code is at yujiri.xyz.

Discussion

markdown guide
 

The simple explanation would be that all runtime errors should throw an exception. The open-ended catch would then need to catch them. Better to throw an exception and let the developer choose how to proceed. I personally think it's better to get an exception than let the code continue when there's been a coding mistake. Much easier to track down bugs.

 

I understand them wanting to have name errors be catchable, but they don't have to be part of the standard exception class. Why couldn't NameError just not be a subtype of StandardError or Exception? You could still catch it if you wanted with except BaseException or except (Exception, NameError).

 

Clojure doesn't, but then again the "interpreter"/compiler will simply refuse to parse your code if it contains unknown identifiers. So in that case it simply isn't a runtime error.

 

Then again, Clojure is a Lisp family member. Lisp will never fail, Lisp will never die.

 

Dynamic languages are like Bethesda, it is a feature, not a bug.