파이썬3 모든 exception의 메세지 내용 추출
except BaseException as e:
str(e)
로 가져다 쓰면 된다.
거의 모든 Exception이 잡히는것 같다.
str(e)
로 가져다 쓰면 된다.
거의 모든 Exception이 잡히는것 같다.
You should not use a blanket
except
, but instead catch Exception
:try:
myFunction()
except Exception:
print "Error running myFunction()"
The
Exception
class is the base class for most exceptions, but not SystemExit
. Together with GeneratorExit
and KeyboardInterrupt
, SystemExit
is a subclass of BaseException
instead:BaseException
+-- SystemExit
+-- KeyboardInterrupt
+-- GeneratorExit
+-- Exception
+-- Everything else
Exception
on the other hand is also a subclass of BaseException
, and the rest of the Python exception classes are derived from it rather than BaseException
directly. Catching Exception
will catch all those derived exceptions, but not the sibling classes.
See the Exception Hierarchy.
As such you should only very rarely use a blanket
except:
statement. Alway try to limit an except handler to specific exceptions instead, or at most catch Exception
.