Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 673
peter.petrov Avatar asked Mar 16 '20 15:03

peter.petrov


People also ask

What is the difference between exceptions and errors?

1. The error indicates trouble that primarily occurs due to the scarcity of system resources. The exceptions are the issues that can appear at runtime and compile time.

Is exception an error in Python?

The code that follows the except statement is the program's response to any exceptions in the preceding try clause. As you saw earlier, when syntactically correct code runs into an error, Python will throw an exception error. This exception error will crash the program if it is unhandled.

What are the 3 types of errors in Python?

There are mainly three kinds of distinguishable errors in Python: syntax errors, exceptions and logical errors.

What are the error in Python?

In python there are three types of errors; syntax errors, logic errors and exceptions.


2 Answers

  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".

like image 180
ShadowRanger Avatar answered Sep 22 '22 22:09

ShadowRanger


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.

like image 28
Ido Avatar answered Sep 21 '22 22:09

Ido