0

In the Java world, we know that the exceptions are classified into checked vs runtime and whenever something throws a checked exception, the caller of that something will be forced to handle that exception, one way or another. Thus the caller would be well aware of the fact that there is an exception and be prepared/coded to handle that.

But coming to Python, given there is no concept of checked exceptions (I hope that is correct), how does the caller of something know if that something would throw an exception or not? Given this "lack of knowledge that an exception could be thrown", how does the caller ever know that it could have handled an exception until it is too late?

Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
jark
  • 305
  • 1
  • 3
  • 11
  • 2
    Presumably the same way a Java programmer knows that something throws an unchecked exception ;-) –  Jun 16 '14 at 18:22
  • Hmm, that doesn't help. I am only referring to "checked exceptions". Unchecked exceptions (RuntimeException) in java is a totally different category, which is caused most probably by user error or programming error. But in checked exceptions that isn't the case most of the times. So I clearly know when to put my code in a try catch block. But that would be hard for me to know in python. That is where my question stems from. – jark Jun 16 '14 at 18:24
  • https://wiki.python.org/moin/HandlingExceptions (contains some information of Python's exception handling idioms contrasted to other languages). – ChristopheD Jun 16 '14 at 18:24
  • Quite a few programmers eschew checked exception and use them for everything, not just for "user error or programming error". Plus, "user errors" also have to be handled, so you still need to recognize and deal with unchecked exceptions. –  Jun 16 '14 at 18:29
  • @delnan Hmm, the RuntimeExceptions are caused by "programming mistake" and are within the "control" of the programmer. That is why they are not forced, because they could have been avoided in the first place with careful programming. But checked exceptions on the other hand are not in programmer's control, since they happen because of unexpected events (like network outage, file server crash or disk being full) and can't be avoided by the programmer. Hence the programmer is forced to acknowledge/handle those. That is where the distinction comes in IMHO. – jark Jun 16 '14 at 18:38
  • 2
    That's all good and well but to the best of my knowledge it doesn't fully encompass how exceptions are actually used in real Java code. Unchecked exceptions are being thrown for things which are "unexpected" and outside of the control of the user. –  Jun 16 '14 at 18:43

4 Answers4

3

There are no checked exceptions in Python.

  1. Read the module docs.
  2. Read the source.
  3. Discover during testing.
  4. Catch a wide range of exception types if necessary (see below).

For example, if you need to be safe:

try:
   ...
except Exception:
   ...

Avoid using a bare except clause, as it will even catch things like a KeyboardInterrupt.

FogleBird
  • 74,300
  • 25
  • 125
  • 131
0

As far as I know Python (6 years) there isn't anything similar to Java's throws keyword in Python.

Magnun Leno
  • 2,728
  • 20
  • 29
0

how does the caller of something know if that something would throw an exception or not?

By reading the documentation for that something.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • The problem with that is that it isn't fool proof (unlike checked exceptions) where you are forced to be aware of the exception, whether you read the documentation or not! – jark Jun 16 '14 at 18:29
  • @jark: That is a false sense of security. You have to read a module's documentation to know its API. Being forced to check the exception doesn't force you to know how to use the module, so you still need to read the documentation anyway, and if you've read the docs, being forced to check the exceptions is just a pain. – BrenBarn Jun 16 '14 at 18:37
0

Design Principle of Python: it's easier to ask forgiveness than permission

EAFP Easier to ask for forgiveness than permission. This common Python coding style assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false. This clean and fast style is characterized by the presence of many try and except statements. The technique contrasts with the LBYL style common to many other languages such as C.

Basics of Unix Philosophy: Rule of Repair

Repair what you can — but when you must fail, fail noisily and as soon as possible.

The essence of both is to use error handling that allows you to find your bugs quickly and wind up with a much more robust program over the long run.

The practical lesson is to learn what errors you should look for as you develop, and only attempt to catch those in your modules, and only use generic Exception handling as a wrapper.

Community
  • 1
  • 1
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331