Skip to content

Files

Latest commit

 

History

History
37 lines (25 loc) · 689 Bytes

catching-non-exception.md

File metadata and controls

37 lines (25 loc) · 689 Bytes

Pattern: Custom exception does not inherit Exception

Issue: -

Description

All built-in, non-system-exiting exceptions are derived from Exception. All user-defined exceptions should also be derived from this class.

Example of incorrect code:

# Does not inherit from Exception
class CustomException(object):
    pass
    
try:
    1 + 1
except CustomException:
    print("caught")

Example of correct code:

class CustomException(Exception):
    pass
    
try:
    1 + 1
except CustomException:
    print("caught")

Further Reading