Skip to content

Exception (usage)

Tsukini edited this page Aug 19, 2026 · 13 revisions

Table of Contents

Important

The exception code from the libutils will be treated has internal and your custom ones hasexternal

The external code that are a duplicate of internal will just point on the internal and will ignore there setup information

The exceptions are set using a .json and generated into a .hpp using a .py script that will be given

Note

If you use the cpp_project_template (here) many things that will be seen under are already setup

Note

You can also install the libutils locally here and recompile it with your own custom exception (they will appear has internal ones)

Setup - Config .json

Emplacement

Note

I use cmake/config/exceptions/ to store the different .json but you can change it if you want, just remember to edit the const.py file with your custom path and the path used in the cmake

By using the given script .py (next step) the .json that you will create could be anywhere recursively or multiple file while it's in the root path cmake/config/exceptions/

Json Content

Caution

The restriction allowed are: None, Warning, Error, Fatal

{
    "errors": [
        {
            "code": "<code_name>", // used with utils::exception::ExternalCode::<code_name>
            "message": "<error_message>", // Returned by what()
            "info": "<sub_info>", // Return by info(), can be removed and will be equal to "[None]" by default
            "restrictions": ["<restriction1>", "<restriction2>", ...] // Restrict the <code_name> to certain type of error, can be removed and there will be no restriction
        }
    ]
}

Output result std::cout << e.formated() std::endl

[<type>] ...<file_path:hyperlink>:<ligne> (<origin>) -> <message>
-------------------------------------------
<function_demangled> = <info>
-------------------------------------------

Exemple - From libutils

{
    "errors": [
        {
            "code": "ExceptionCodeRestriction",
            "message": "Error during the setup of an exception",
            "info": "Restriction trigerred on a code & type combination",
            "restrictions": ["Fatal", "Error"]
        },
        {
            "code": "Exit",
            "message": "Exit",
            "info": "Exit",
            "restrictions": ["None"]
        },
        {
            "code": "Empty",
            "message": "Empty"
        }
    ]
}
[Error(fatal)] .../src/utils/exception/AException.cpp:39 (utils) -> Error during the setup of an exception
-------------------------------------------
void utils::exception::AException::subinit(void) = Restriction trigerred on a code & type combination
-------------------------------------------

Setup - Script .py

Files: scripts.zip

> sha256sum scripts.zip 
84cc95c2d812bf3d3cea4e2c8959978141306870311404bc364313ff8131e703  scripts.zip

Note

I use cmake/scripts/ to store the different scripts but you can change it if you want, just remember to change the path used in the cmake with your custom path

Setup - Compilation .cmake

Setup the execution of the script before the compilation

file(GLOB_RECURSE EXCEPTION_CONFIG_FILES
    CONFIGURE_DEPENDS
    "${CMAKE_SOURCE_DIR}/cmake/config/exceptions/*.json"
)
set(GENERATED_EXCEPTION_HEADER
    "${CMAKE_SOURCE_DIR}/include/exception/generated_external_exception_header.hpp"
)
add_custom_command(
    OUTPUT ${GENERATED_EXCEPTION_HEADER}
    COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/cmake/scripts/generate_exception_header.py
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
    DEPENDS ${EXCEPTION_CONFIG_FILES} ${CMAKE_SOURCE_DIR}/cmake/scripts/generate_exception_header.py
    COMMENT "Generating exception header (config changed)"
    VERBATIM
)
add_custom_target(generated_external_exception_header
    DEPENDS ${GENERATED_EXCEPTION_HEADER}
)

add_dependencies(${TARGET} generated_external_exception_header)

Include directory needed by the libutils to find your custom exceptions

target_include_directories(${TARGET} PRIVATE include/exception)

PS: don't forget to include the libutils in your compilation ._.

Usage

After all this setup, you should be able to compile your project and use your own exception

This example is based on the promise that you have set an Exit exception that allow the type None

throw utils::exception::NoneException(utils::exception::InternalCode::Exit); // internal exemple
throw utils::exception::NoneException(utils::exception::ExternalCode::Exit); // external exemple

In this case both will throw the same error due to duplicated code