In our JavaScript interpreter, we used exceptions ro implement return statements.
Is this bad? Should we be using Python if we have to do this?
Well, there is a lot of language-level exception handling in the real world.
I don't think it's a reason to avoid Python or claim that one language is more dangerous than another.
Language-level exception handling is super popular.
It's actually goes back to Goodenough in 1975 who introduced a replacement model.
That is an amazing family name. I wish I had that name.
And since then, a number of languages have allowed you to deal with programmer errors
and environmental concerns--both of which are often lump under the category
of exceptions at the language level, and this actually is super handy because the previous
approach involves sort of setting and checking global flags and programmers are very poor
at remembering to do that consistently and correctly.
Even with language level exception handling, it turns out that programmers make a lot of mistakes.
One of the personal hypothesis is that this is because the control flow
for exception handling isn't visible.
When you're looking at the code, you can tell the control is going to flow down to the next line.
if there's a statement in Python it's indented over.
But exceptions really represent this sort of nonlocal jump to somewhere else
and the indenting of the coder, the structure of the code may not make it clear.
And if you're not aware that code can jump out of one line and go to another.
You may forget to reestablish some in variant or close some important file
or finalize a day of the structure by the end.
And in fact, even a number of years ago, when I was doing my dissertation on the subject--
Oh, tragically boring--open source job of programmers tended to make
on the order of 800 mistakes per 4 million lines of code related to exception handling alone.
And in fact, exception handling is so prominent that if you look at an average program
somewhere between 1% and 5% of the text of that program will literally be catch and finally blocks.
That may actually seem low but typically a catcher or finally block, if it's not just empty
will call some error handling routine and in large pieces of software, anywhere between 3 and 46%
of the program is transitively reachable through methods calls or what not
from catch and finally blocks.
If you're interested in writing large robust pieces of software that get deployed in the real world,
You will spend quite a bit of time dealing with handling errors.
Language-level exception handling is at least for now
much better than the alternative for that sort of thing.
Is this a reason to avoid Python?
We saw in class that we could use something like exception handling to deal with
interpreting JavaScript return statements in our Python JavaScript interpreter?
No, I wouldn't devoid Python as a result.
In fact, even nominally friendlier languages like Java are potentially worse.
You may not be aware of this Java actually still has GOTOs--go check it out.
You can put in labels and then reuse the brake statement if you're given an argument
to behave like GOTOs, so even though nominally more typed safe,
more structured languages are supposed to avoid that sort of thing.
In practice, a lot of real world languages be the Python or be the Java have their secret dark
corners allowing you to write unstructured spaghetti code.
This is nothing particularly bad about Python or particularly bad about JavaScript.
It really gets back to this almost philosophical issue that sometimes when you detect an error,
you don't have enough time, you don't have enough context to deal with it.
That's really the motivation for exception handling.
When I'm in the middle of some low level codes that fails to write something to the disc
or fails to send something over the network, I don't know if should retry.
Why not? Depends on the rest of the application semantics.
If it's some sort of network maneuver may be I want to sleep for five seconds and try again,
or if it's something like voice over IP like Skype may be I don't want to bother,
may be I'll just wait for the next packet to arrive.
I can't know unless I know more about the application semantics.
So exceptions allow me to throw that notice of an error up and hopefully someone else
who has more information will catch it--that's generic to the experience of programming.
It's not particular to any one language so I wouldn't hold it against Python or Java or even JavaScript.