Throwing Errors and Tracebacks
While Call Stack Management handles the execution flow and context, we need specific tools to originate and visualize errors. This section provides the tools to throw errors, checking the error states, and dumping traceback when error occurs.
API
THROW Macro
Wrapper for throwing an error with the current call stack.
void THROW(CTB_Error ctb_error, const char *restrict msg);Pass "" to msg when error message is not needed.
THROW simply records the error. It has no
effect if you forget to do error checking.
Expands to
do \
{ \
ctb_throw_error(ctb_error, __FILE__, __LINE__, __func__, msg); \
} while (0)Parameters
| Name | Type | Description |
|---|---|---|
error | CTB_Error | The error type. |
msg | const char* | Error message. |
Usage
THROW(CTB_VALUE_ERROR, "");
THROW(CTB_MEMORY_ERROR, "Out of memory!");THROW_FMT Macro
Wrapper for raising an error with formatted message with the current call stack.
void THROW_FMT(CTB_Error ctb_error, const char *restrict msg, ...);THROW_FMT simply records the error. It has no
effect if you forget to do error checking.
Expands to
do \
{ \
ctb_throw_error_fmt( \
ctb_error, __FILE__, __LINE__, __func__, msg, __VA_ARGS__ \
); \
} while (0)Parameters
| Name | Type | Description |
|---|---|---|
error | CTB_Error | The error type. |
msg | const char* | Error message. |
... | N/A | Additional arguments for formatting the message. |
Usage
#include <stdio.h>
#include "c_traceback.h"
#define FILE_PATH "../test.txt"
void open_file(const char *file_name)
{
FILE *file = fopen(file_name, "r");
if (!file)
{
THROW_FMT(CTB_FILE_NOT_FOUND_ERROR, "Failed to open file: \"%s\"", file_name);
return;
}
/* Do something */
fclose(file);
}
int main(void)
{
TRY_GOTO(open_file(FILE_PATH), error);
/* Do something */
return 0;
error:
ctb_dump_traceback();
return 1;
}Output
────────────────────────────────────────────────────────────────────────────────
Traceback (most recent call last):
(#00) File "/home/alvinng/Desktop/c_traceback/examples/example_open_file.c", line 21 in main:
open_file(FILE_PATH)
(#01) File "/home/alvinng/Desktop/c_traceback/examples/example_open_file.c", line 12 in open_file:
<Error thrown here>
FileNotFoundError: Failed to open file: "../test.txt"
────────────────────────────────────────────────────────────────────────────────ctb_check_error Function
Check if any error has occurred.
bool ctb_check_error(void);Returns
| Name | Type | Description |
|---|---|---|
error | bool | Whether an error has occurred |
Usage
if(ctb_check_error())
{
goto error;
}ctb_check_error Function
Clear all recorded errors.
void ctb_clear_error(void);See No exception handling?
ctb_dump_traceback Function
Dump the traceback of all recorded errors to stderr and clear errors.
void ctb_dump_traceback(void);Usage
#include <stdio.h>
#include "c_traceback.h"
#define FILE_PATH "../test.txt"
void open_file(const char *file_name)
{
FILE *file = fopen(file_name, "r");
if (!file)
{
THROW_FMT(CTB_FILE_NOT_FOUND_ERROR, "Failed to open file: \"%s\"", file_name);
return;
}
/* Do something */
fclose(file);
}
int main(void)
{
TRY_GOTO(open_file(FILE_PATH), error);
/* Do something */
return 0;
error:
ctb_dump_traceback();
return 1;
}Output
────────────────────────────────────────────────────────────────────────────────
Traceback (most recent call last):
(#00) File "/home/alvinng/Desktop/c_traceback/examples/example_open_file.c", line 21 in main:
open_file(FILE_PATH)
(#01) File "/home/alvinng/Desktop/c_traceback/examples/example_open_file.c", line 12 in open_file:
<Error thrown here>
FileNotFoundError: Failed to open file: "../test.txt"
────────────────────────────────────────────────────────────────────────────────ctb_log_traceback Function
Log the traceback of all recorded errors to stderr.
void ctb_log_traceback(void);ctb_dump_traceback unless the error
states are needed for further use.ctb_log_call_stack Function
Log the current call stack to stderr.
void ctb_log_call_stack(void);