Avoid exceptions
This commit is contained in:
15
style.md
15
style.md
@@ -301,8 +301,7 @@ arena.reset(); // Reset arena memory
|
||||
|
||||
### Error Reporting
|
||||
- **Return codes** for expected errors
|
||||
- **Exceptions** only for exceptional circumstances
|
||||
- **fprintf + abort()** for unrecoverable errors
|
||||
- **Avoid exceptions** - If we can't uphold the component's contract, perror/fprintf then abort. If we want to try to recover, change the component's contract to allow returning an error code.
|
||||
- **Error messages are for humans only** - never parse error message strings programmatically
|
||||
- **Error codes are the contract** - use enums/codes for programmatic error handling
|
||||
```cpp
|
||||
@@ -317,9 +316,13 @@ if (result == ParseResult::InvalidJson) {
|
||||
// Bad: Don't test or parse error message strings
|
||||
// CHECK(parser.get_error() == "Expected '}' at line 5"); // BRITTLE!
|
||||
|
||||
if (!memory) {
|
||||
std::fprintf(stderr, "ArenaAllocator: Failed to allocate memory\n");
|
||||
std::abort();
|
||||
// System resource failures: abort immediately
|
||||
void ArenaAllocator::allocate() {
|
||||
void* memory = malloc(size);
|
||||
if (!memory) {
|
||||
perror("malloc");
|
||||
std::abort(); // Process is likely in bad state
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -480,4 +483,4 @@ cmake .. -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
|
||||
- **Static analysis** tools for code quality
|
||||
- **Address sanitizer** for memory safety testing
|
||||
|
||||
This style guide reflects the existing codebase patterns and should be followed for all new code contributions to maintain consistency and readability.
|
||||
This style guide reflects the existing codebase patterns and should be followed for all new code contributions to maintain consistency and readability.
|
||||
|
||||
Reference in New Issue
Block a user