Risky catches increase production risks
bad
class MyException(object):
def __init__(self, code, message):
self.code = code
self.message = message
try:
if 1 != 0:
raise MyException(42, "1 != 0 Exception")
except MyException as e:
print(e.message)
good
lass MyException(Exception):
def __init__(self, code, message):
self.code = code
self.message = message
try:
if 1 != 0:
raise MyException(1244, "1 != 0 Exception")
except MyException as e:
print(e.message)
Why you should care
BaseException
 at the lowest level. User-defined exception classes should actually inherit from Exception
; they shouldn’t directly inherit from BaseException
. Python requires all exception classes to inherit from a base exception superclass to ensure that they all meet the minimum required interface needed to be useful.
Highlight will consider that each classes whose name ends with “Exception” or “Error” is an exception class. This will be statically right, and is acceptable because Highlight is a statistical tool.
Through this rule, Highlight will check that all exceptions classes are derived from the builtin BaseException (but never directly !).
Business Impacts
CAST recommendations
References
https://www.quantifiedcode.com/knowledge-base/correctness/Inherit%20from%20%60BaseException%60%20for%20all%20custom%20exceptions/72tTnTsJ
About CAST and Highlight’s Code Insights
Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.