Giter Site home page Giter Site logo

5cover / c-exceptions Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 2.0 139 KB

Macros for throwing and handling exceptions in C, in a try/catch/finally-like fashion.

License: The Unlicense

C 100.00%
c exception-handling exceptions exception try-catch try-catch-finally

c-exceptions's Introduction

C-exCeptions

Static library containing macros for throwing and handling exceptions in C based on the C# syntax.

Unhandled exceptions result in a call to abort(). To respond to unhandled exceptions, the SIGABRT signal can be handled or a try/catchAll block can be defined in the main function.

Usage

Interface reference:

  • throw: throw an exception
    1. id - ID of the exception. Can be an ExceptionID value or an integer.
    2. message - Message of the exception. Must be a string (char *).
  • try: define a try block
  • catch: catch an exception with a specific ID
    1. targetId - ID of the exception to catch. Can be an ExceptionID value or an integer.
    2. varName - Name of the variable (of type Exception const *const) that points the catched exception.
  • catchWhen: catch an exception with a specific ID only when the provided boolean expression evaluates to true
    1. targetId - ID of the exception to catch. Can be an ExceptionID value or an integer.
    2. varName - Name of the variable (of type Exception const *const) that points the catched exception.
  • catchAll: catch any exception
    1. varName - Name of the variable (of type Exception const *const) that points the catched exception.
  • catchAllWhen: catch any exception when the provided boolean expression evaluates to true
    1. varName - Name of the variable (of type Exception const *const) that points the catched exception.
  • rethrow: rethrow an exception from a catch block.
  • finally: defines a block to execute regardless of whether an exception occured or not.

See the comments in example for a walk-through of the code.

Known issues

  • The try macro cannot be used in a single-statement scope (i.e. if statement without braces). This is not a problem since it is useless without catch or finally blocks.
  • An exception variable name has to be specified when defining a catch block. You can use _ as the identifier if you're not planning to use it. Unused variable warnings will be hidden if GNU C is supported (with the unused attribute).
  • The try-catch stack is defined as thread-local, so exceptions will not bubble up outside of their original thread.

Reserved identifiers

All reserved identifiers are prefixed by _ex_.

Contributing

This is mostly an experiment with advanced macro features and the setjmp/longjmp.

c-exceptions's People

Contributors

5cover avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.