Illegal exceptions can cause Production Risks

Software ResiliencyCode Resiliency

Risky catches increase production risks

This code insight counts one violation each time a class whose name is ending with Exception or Error is not derived from another class whose name is ending with Exception or Error, excluding BaseException.

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)

5362

Why you should care

Classes used for handling and representing exceptions must always inherit from 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

Production Risk

CAST recommendations

References

https://www.quantifiedcode.com/knowledge-base/correctness/Inherit%20from%20%60BaseException%60%20for%20all%20custom%20exceptions/72tTnTsJ

5362

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.

See featuresHow it works