Unused exception object¶
ID: py/unused-exception-objectKind: problemSecurity severity: Severity: errorPrecision: very-highTags: - quality - reliability - error-handlingQuery suites: - python-security-and-quality.qls
Click to see the query in the CodeQL repository
Creating a new exception object is no different from creating any other object. The exception needs to be raised to have an effect.
Recommendation¶
Insert araise before the exception.
Example¶
In this example, the first functiondo_action_forgotten_raise() silently ignores any erroneous input. Whereas, the second functiondo_action correctly raises an exception if the ‘action’ is not understood.
defdo_action_forgotten_raise(action):ifaction=="go":start()elifaction=="stop":stop()else:ValueError(action)defdo_action(action):ifaction=="go":start()elifaction=="stop":stop()else:raiseValueError(action)