Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between except and except BaseException

Tags:

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?

like image 289
stefan.stt Avatar asked Jun 20 '17 14:06

stefan.stt


People also ask

What is the difference between exception and BaseException in Python?

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.

Does BaseException catch all exceptions?

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.

When would you use try and except block?

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.

What is except exception as e?

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.


2 Answers

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.

like image 95
Martijn Pieters Avatar answered Sep 29 '22 13:09

Martijn Pieters


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.

like image 44
EZLearner Avatar answered Sep 29 '22 13:09

EZLearner