Tag Archives: Python3.7 Capture exception error

Python3.7 Capture exception error: Too broad exception clause

In Pycharm, using try…exception will result in the Too broad exception clause… warning

The reason for this error is that the exceptions caught in the past are generalized and not to specific exceptions, which lack relevance and can be solved by specifying the exact exception type.
such as:

Baseexception: the base class of all exceptions
systemexit: the interpreter requests to exit
keyboardinterrupt: the user interrupts execution (usually input ^c)

error: base class of general error
stopiteration: the iterator has no more values
generatorexit: the generator has an exception to notify the exit
standarderror: base class of all built-in standard exceptions
arithmeticerror: base class of all numerical calculation errors
floatingpointerror: floating-point calculation error
overflowerror: numerical operation exceeds the maximum limit
zerodivisionerror: division (or modulo) Zero (all data types)
assertionerror: assertion statement failure
attributeerror: the object does not have this attribute
eofilter: there is no built-in input, Reaching EOF tag
environmenterror: base class of operating system error
ioerror: input/output operation failure
oserror: operating system error
windowserror: system call failure
importerror: import module/object failure
lookuperror: base class of invalid data query
indexerror: this index is not in the sequence
keyerror: this key is not in the mapping
memoryerror: memory overflow error (not fatal for Python interpreter)
nameerror: undeclared/initialized object (no attributes)
unboundlocalerror: accessing uninitialized local variables
referenceerror: weak reference Trying to access an object that has been garbage collected
runtimeerror: general runtime error
notimplementederror: unimplemented method
syntaxerror: Python syntax error
indentationerror: indentation error
taberror: mixed use of tab and space
systemerror: general interpreter system error
typeerror: invalid operation on type
valueerror: passing in invalid parameters
Unicode error: Unicode related Error of
Unicode decodeerror: error in Unicode decoding
Unicode encodeerror: error in Unicode encoding
Unicode translateerror: error in Unicode conversion
warning: base class of warning
deprecationwarning: warning about deprecated features
futurewarning: warning about future semantic changes in construction
overflowwarning: old warning about automatic promotion to long integer (long) Warning of
pendingdeprecationwarning: warning about feature will be discarded
runtimewarning: warning about suspicious runtime behavior
syntaxwarning: warning about suspicious syntax
userwarning: warning generated by user code

If you are not sure about the possible errors, or you need to use exception and pycharm is not allowed to complain, how should you solve it
Method 1: turn off the option to detect exceptions in code detection in the compiler
Method 2: add # noinspection PyBroadException before the try statement

# noinspection PyBroadException
try:
       pass
except Exception as e:
       pass