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:
There are syntax errors which of course cause your program not to be able to start at all. Right?
"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?
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... ?
"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.
Warnings also derive from Exception. So are warnings fatal or system-exiting or none of these?
Where does the AssertionError
fit into all of this? Is it fatal or system exiting?
How does one know or specify that some Exception class represents fatal or system-exiting exception?
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.
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.
There are mainly three kinds of distinguishable errors in Python: syntax errors, exceptions and logical errors.
In python there are three types of errors; syntax errors, logic errors and exceptions.
SyntaxError
isn't catchable except in cases of dynamically executed code (via eval
/exec
), because it occurs before the code is actually running.os._exit
can forcibly kill the process, but it does so by bypassing the exception mechanism.BaseException
, but not Exception
. But they can be caught just like any other exceptionException
means they're not in the "system-exiting" categoryAssertionError
is just another Exception
child class, so it's not "system exiting". It's just tied to the assert
statement, which has special semantics.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".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With