except Exception:vsexcept BaseException::The difference between catching
ExceptionandBaseExceptionis that according to the exception hierarchy exception like SystemExit, KeyboardInterrupt and GeneratorExit will not be caught when usingexcept Exceptionbecause they inherit directly fromBaseException.except:vsexcept BaseException::The difference between this two is mainly in python 2 (AFAIK), and it's only when using an old style class as an Exception to be raised, in this case only expression-less except clause will be able to catch the exception eg.
class NewStyleException(Exception): pass try: raise NewStyleException except BaseException: print "Caught" class OldStyleException: pass try: raise OldStyleException except BaseException: print "BaseException caught when raising OldStyleException" except: print "Caught"
except Exception:vsexcept BaseException::The difference between catching
ExceptionandBaseExceptionis that according to the exception hierarchy exception like SystemExit, KeyboardInterrupt and GeneratorExit will not be caught when usingexcept Exceptionbecause they inherit directly fromBaseException.except:vsexcept BaseException::The difference between this two is mainly in python 2 (AFAIK), and it's only when using an old style class as an Exception to be raised, in this case only expression-less except clause will be able to catch the exception eg.
class NewStyleException(Exception): pass try: raise NewStyleException except BaseException: print "Caught" class OldStyleException: pass try: raise OldStyleException except BaseException: print "BaseException caught when raising OldStyleException" except: print "Caught"
If you need to catch all exceptions and do the same stuff for all, I'll suggest you this :
try:
#stuff
except:
# do some stuff
If you don't want to mask "special" python exceptions, use the Exception base class
try:
#stuff
except Exception:
# do some stuff
for some exceptions related management, catch them explicitly :
try:
#stuff
except FirstExceptionBaseClassYouWantToCatch as exc:
# do some stuff
except SecondExceptionBaseClassYouWantToCatch as exc:
# do some other stuff based
except (ThirdExceptionBaseClassYouWantToCatch, FourthExceptionBaseClassYouWantToCatch) as exc:
# do some other stuff based
The exception hierarchy from the python docs should be a usefull reading.
python 3.x - Inheriting from BaseException vs Exception - Stack Overflow
Exception handling in python
Why is it recommended to derive from Exception instead of BaseException class in Python? - Stack Overflow
What is the problem with catching the base Exception class when it is needed?
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.
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.
What is the best way to handle exceptions in python? What are the best practices?
Context: I have seen the below alert for one project but not sure if we should add more exceptions and handle everything separately or do something else
Except block directly handles BaseException. Handling 'BaseException' means that system exits and keyboard interrupts may be mis-handled.