What is the difference between those two:
except: # do something
and
except BaseException as be: print(be)
I mean in the first case all possible exception are caught, but is this true for the second?
Also can the error message be printed using the first case?
User can derive their own exception from the Exception class, or from any other child class of Exception class. The BaseException is the base class of all other exceptions. User defined classes cannot be directly derived from this class, to derive user defied class, we need to use Exception class.
All exception classes in Python derive from BaseException . BaseException has three important subclasses, Exception from which all errors and normal exceptions derive, KeyboardInterrupt which is raised when the user interrupts the program from the keyboard and SystemExit which is raised by the sys.
The try block lets you test a block of code for errors. The except block lets you handle the error. The else block lets you execute code when there is no error. The finally block lets you execute code, regardless of the result of the try- and except blocks.
In contrast, the except Exception as e statement is a statement that defines an argument to the except statement. e in the latter statement is utilized to create an instance of the given Exception in the code and makes all of the attributes of the given Exception object accessible to the user.31-Dec-2021.
Practically speaking, there is no difference between except:
and except BaseException:
, for any current Python release.
That's because you can't just raise any type of object as an exception. The raise
statement explicitly disallows raising anything else:
[...] raise evaluates the first expression as the exception object. It must be either a subclass or an instance of
BaseException
.
Bold emphasis mine. This has not always been the case however, in older Python releases (2.4 and before) you could use strings as exceptions too.
The advantage then is that you get to have easy access to the caught exception. In order to be able to add as targetname
, you must catch a specific class of exceptions, and only BaseException
is going to do that.
You can still access the currently active exception by using sys.exc_info()
though:
except: be = sys.exc_info()[1]
Pick what you feel is more readable for your future self and for your colleagues.
The accepted answer is incorrect incomplete (at least for Python 3.6 and above).
By catching Exception
you catch most errors - basically all the errors that any module you use might throw.
By catching BaseException
, in addition to all the above exceptions, you also catch exceptions of the types SystemExit
, KeyboardInterrupt
, and GeneratorExit
.
By catching KeyboardInterrupt
, for example, you may stop your code from exiting after an initiated exit by the user (like pressing ^C
in the console, or stopping launched application on some interpreters). This could be a wanted behavior (for example - to log an exit), but should be used with extreme care!
In the above example, by catching BaseException
, you may cause your application to hang when you want it to exit.
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