peter.petrov
peter.petrov

Reputation: 39477

Exceptions vs Errors in Python

I come from Java where Exceptions and Errors are quite different things and they both derive from something called Throwable. In Java normally you should never try to catch an Error.

In Python though it seems the distinction is blurred.

So far after reading some docs and checking the hierarchy I have the following questions:

  1. There are syntax errors which of course cause your program not to be able to start at all. Right?

  2. "Errors detected during execution are called exceptions and are not unconditionally fatal" (per the tutorial). What does "fatal" mean here? Also, some objects like AttributeError are (by the above definition) actually exceptions even though they contain Error in their names, is that conclusion correct?

  3. Some classes derive from Exception but contain Error in their name. Isn't this confusing? But even so it means that Error in the name is in no way special, it's still an Exception. Or not... ?

  4. "All built-in, non-system-exiting exceptions are derived from [Exception]" (quote from here)
    So which ones are system-exiting exceptions and which ones are not? It is not immediately clear. All user-defined exceptions should also be derived from Exception. So basically as a beginner do I need to worry about anything else but Exception? Seems like not.

  5. Warnings also derive from Exception. So are warnings fatal or system-exiting or none of these?

  6. Where does the AssertionError fit into all of this? Is it fatal or system exiting?

  7. How does one know or specify that some Exception class represents fatal or system-exiting exception?

Upvotes: 41

Views: 17841

Answers (2)

Ido
Ido

Reputation: 168

Exceptions are designed for the programmer to know how to handle them such as outOfRange Once an exception arises the programmer has to decide how to handle it and the code can continue to operate relatively smoothly

On the other hand an error indicates a problem that the programmer could not foresee as an import error or a memory error Errors can still be addressed and ensure that the software continues to run but apparently not everything will be able to continue to work smoothly.

Upvotes: 2

ShadowRanger
ShadowRanger

Reputation: 155744

  1. Yes. SyntaxError isn't catchable except in cases of dynamically executed code (via eval/exec), because it occurs before the code is actually running.
  2. "Fatal" means "program dies regardless of what the code says"; that doesn't happen with exceptions in Python, they're all catchable. os._exit can forcibly kill the process, but it does so by bypassing the exception mechanism.
  3. There is no difference between exceptions and errors, so the nomenclature doesn't matter.
  4. System-exiting exceptions derive from BaseException, but not Exception. But they can be caught just like any other exception
  5. Warnings behave differently based on the warnings filter, and deriving from Exception means they're not in the "system-exiting" category
  6. AssertionError is just another Exception child class, so it's not "system exiting". It's just tied to the assert statement, which has special semantics.
  7. Things deriving from BaseException but not Exception (e.g. SystemExit, KeyboardInterrupt) are "not reasonable to catch" (or if you do catch them, it should almost always be to log/perform cleanup and rethrow them), everything else (derived from Exception as well) is "conditionally reasonable to catch". There is no other distinction.

To be clear, "system-exiting" is just a way of saying "things which except Exception: won't catch"; if no except blocks are involved, all exceptions (aside from warnings, which as noted, behave differently based on the warnings filter) are "system-exiting".

Upvotes: 42

Related Questions