Error and Warning Types
Inspired by Python, we provide the following error and warning hierarchy.
It is for reference only as we decided
not to implement exceptions catching.
See No exception handling?
See No exception handling?
Error Hierarchy
To throw an error with a type below, add CTB as prefix and turn it into
screaming snake case. For example, ValueError becomes CTB_VALUE_ERROR.
Error
├── SystemError
├── ResourceError
├── ValueError
├── SignalError
│ ├── SignalAbort
│ ├── SignalSegmentationFault
│ ├── SignalInvalidInstruction
│ ├── SignalTermination
│ ├── SignalFloatingPointException
│ └── SignalKeyboardInterrupt
├── MemoryError
│ ├── OutOfMemoryError
│ └── BufferError
├── PointerError
│ └── NullPointerError
├── RuntimeError
│ └── NotImplementedError
├── LookupError
│ ├── IndexError
│ └── KeyError
├── MathError
│ ├── MathDomainError
│ ├── MathOverflowError
│ ├── MathUnderflowError
│ └── ZeroDivisionError
├── OSError
│ ├── BlockingIOError
│ ├── ChildProcessError
│ ├── FileExistsError
│ ├── FileNotFoundError
│ ├── InterruptedError
│ ├── IsADirectoryError
│ ├── NotADirectoryError
│ ├── PermissionError
│ ├── ProcessLookupError
│ └── TimeoutError
├── NetworkError
│ ├── ConnectionFailedError
│ └── HostUnreachableError
├── ConcurrencyError
│ └── ThreadCreationError
├── ParseError
│ ├── EncodingError
│ ├── InvalidFormatError
│ ├── SyntaxError
│ ├── UnexpectedEOFError
│ └── ChecksumMismatchError
└── DynamicLinkError
└── LibraryLoadErrorWarning Hierarchy
Same as error, simply add CTB as prefix and turn it into
screaming snake case. For example, ResourceWarning becomes CTB_RESOURCE_WARNING.
Warning
├── DeprecationWarning
├── PerformanceWarning
├── ResourceWarning
├── PrecisionWarning
├── CompatibilityWarning
├── SecurityWarning
├── ParseWarning
└── UserWarningAdding your own error / warning
It is easy to add your own error / warning code. Let’s say we want to add CustomError1,
simply follow the steps:
Add error code at include/c_traceback/error_codes.h
Add custom error code at the bottom of typedef enum CTB_Error:
typedef enum CTB_Error
{
CTB_SUCCESS = 0,
CTB_UNKNOWN_ERROR = -1,
...
/* --- Dynamic Link Errors (900-999) --- */
CTB_DYNAMIC_LINK_ERROR = 900,
CTB_LIBRARY_LOAD_ERROR,
/* --- 1000+: application-specific --- */
CUSTOM_ERROR_1 = 1000,
}Add name mapping at src/error_codes.c
Add the mapping at the bottom of error_to_string(CTB_Error error):
const char *error_to_string(CTB_Error error)
{
switch (error)
{
case CTB_SUCCESS: return "Success";
case CTB_UNKNOWN_ERROR: return "Unknown Error";
...
/* Dynamic Link */
case CTB_DYNAMIC_LINK_ERROR: return "DynamicLinkError";
case CTB_LIBRARY_LOAD_ERROR: return "LibraryLoadError";
/* Application */
case CUSTOM_ERROR_1: return "CustomError1";
default: return "Unrecognized Error Code";
}
}Now you should be able to use it directly:
────────────────────────────────────────────────────────────────────────────────
Traceback (most recent call last):
(#00) File "/home/alvinng/Desktop/project/project.c", line 6 in main:
<Error thrown here>
CustomError1
────────────────────────────────────────────────────────────────────────────────Last updated on