except Exception:
vsexcept BaseException:
:The difference between catching
Exception
andBaseException
is that according to the exception hierarchy exception like SystemExit, KeyboardInterrupt and GeneratorExit will not be caught when usingexcept Exception
because 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"