Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
forked from http://ldeniau.web.cern.ch/ldeniau/oopc.html#Exception "Exception in C" Laurent Deniau
  • Loading branch information
TheTechsTech committed Apr 19, 2024
0 parents commit d8acd10
Show file tree
Hide file tree
Showing 32 changed files with 1,920 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
52 changes: 52 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Prerequisites
*.d

# Object files
*.o
*.ko
*.obj
*.elf

# Linker output
*.ilk
*.map
*.exp

# Precompiled Headers
*.gch
*.pch

# Libraries
*.lib
*.a
*.la
*.lo

# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib

# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex

# Debug files
*.dSYM/
*.su
*.idb
*.pdb

# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Lawrence Stubbs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
86 changes: 86 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
Exceptions in ISO C89

Copyright (c) 2001+ Laurent Deniau, [email protected]

LAST RELEASE:
http://cern.ch/laurent.deniau/oopc.html

INSTALL:
mkdir exception; cd exception
tar xvzf exception.tgz
-- edit exception.h to configure the Thread Locale Storage
cp exception.h path-to-include
cp exception.c path-to-source

TESTSUITE:
-- edit testsuite.sh file to configure your compiler and enable threads
./testsuite.sh

SUPPORTED SYSTEMS:
- Debian Etch x86-32, gcc 3.x and 4.x
- Fedora x86-64, gcc 3.x and 4.x
- to be continued..

COMMENTS:
- Look at the examples for the syntax and the semantic.

- The semantic of TRY-CATCH-FINALLY blocks is the same as in Java and C#,
*except* that the use of return, break, continue, goto or longjump to
jump inside or outside these blocks is *FORBIDDEN* (unchecked error!).

- Throwing (rethrowing) exceptions within CATCH and FINALLY blocks is valid.

- Combining both CATCH and FINALLY blocks in the same TRY-ENDTRY
scope is equivalent to:

TRY TRY
TRY
.. ..
CATCH(E1) CATCH(E1)
.. ..
CATCH(E2) CATCH(E2)
.. ..
CATCH_ANY() CATCH_ANY()
.. ..
ENDTRY
FINALLY() FINALLY()
.. ..
ENDTRY ENDTRY

except that:
- 1st case is more efficient.
- 2nd case FINALLY() doesn't have access to the inner TRY-ENDTRY exception.

- CATCH and FINALLY blocks are optional, but it makes sense to have
at least one CATCH block. Within these blocks, THROW(E) and RETHROW()
can be freely used.

- If an uncaught exception reaches ENDTRY, it will be automatically
rethrown. If it is the last TRY-ENDTRY, ex_terminate() will be called.

- THROWLOC() can specify its location (automatic by default) after the
exception argument (i.e. THROWLOC(ex,file,line)).

- exception are const pointers to strings literals and can be used as such
(e.g. printed).

- PRT(ptr,del) protects the pointer ptr (not the object pointed) against
raised exceptions, therefore if the object pointed by ptr changes before
UNPRT(), the new object will be automatically protected. If ptr is
not nul, del(ptr) will be invoked during stack unwinding. During stack
unwinding, ex_uncaught_exception() returns a non-zero value, 0 otherwise.

- UNPRT(ptr) unprotects ptr and all objects protected after.

- PRT()/UNPRT() work like a stack PUSH()/POP() of protected objects.

- A keyword will be prefixed with EX_ if EX_DISABLE_keyword is defined
before including exception.h (i.e. name clash).

- To convert a signal into an exception, one can use
extern const char EX_NAME(sig_val)[];
ex_signal(SIGVAL, EX_NAME(sig_val));
or simply
ex_signal_std()
which converts some common signals (see exception.c) into exceptions
if they are available (i.e. defined in signal.h).
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# c-exceptions
An simple exception/signal handling interface for C
36 changes: 36 additions & 0 deletions ex-00sp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <stdio.h>
#include <time.h>
#include "exception.h"

enum { n_loop = 25000000 };

EXCEPTION(division_by_zero);

static void test(void)
{
TRY {
}
CATCH(bad_alloc) {
}
CATCH(division_by_zero) {
}
FINALLY() {
}
ENDTRY
}

int main(void)
{
double t0,t1;
int i;

t0 = clock();

for (i = 0; i<n_loop; i++)
test();

t1 = clock();
printf("timing: %9d loops = %.2f sec\n", n_loop, (t1-t0)/CLOCKS_PER_SEC);

return 0;
}
70 changes: 70 additions & 0 deletions ex-00ts.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include "exception.h"

enum { n_th = 4, n_loop = 25000000 };

EXCEPTION(division_by_zero);

static void tst(void)
{
TRY {
}
CATCH(bad_alloc) {
}
CATCH(division_by_zero) {
}
FINALLY() {
}
ENDTRY
}

static void* test(void *_)
{
int i;

(void)_;

for (i = 0; i<n_loop/n_th; i++)
tst();

return 0;
}

int main(void)
{
pthread_attr_t attr;
pthread_t th[n_th];
double t0,t1;
int i;

pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE);

t0 = clock();

for (i = 0; i < n_th; i++) {
/* fprintf(stderr, "Starting thread %d\n", i); */
if (pthread_create(&th[i],&attr,test,0)) {
fprintf(stderr, "unable to create thread %d\n", i);
exit(EXIT_FAILURE);
}
}

for (i = 0; i < n_th; i++) {
/* fprintf(stderr, "Waiting thread %d\n", i); */
if (pthread_join(th[i],NULL)) {
fprintf(stderr, "unable to join thread %d\n", i);
exit(EXIT_FAILURE);
}
}

t1 = clock();
printf("timing: %9d loops = %.2f sec\n", n_loop, (t1-t0)/CLOCKS_PER_SEC);

pthread_attr_destroy(&attr);

return 0;
}
20 changes: 20 additions & 0 deletions ex-01.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <stdio.h>
#include "exception.h"

EXCEPTION(division_by_zero);

static int idiv(int a, int b)
{
if (b == 0)
THROW(division_by_zero);

return a/b;
}

int main(void)
{
idiv(1,0);
printf("never reached\n");

return 0;
}
32 changes: 32 additions & 0 deletions ex-02.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <stdio.h>
#include "exception.h"

EXCEPTION(division_by_zero);

static int idiv(int a, int b)
{
if (b == 0)
THROW(division_by_zero);

return a/b;
}

int main(void)
{
TRY {
idiv(1,0);
printf("never reached\n");
}
CATCH(bad_alloc) {
printf("catch: exception %s (%s:%d) caught\n", ex, ex_file, ex_line);
}
FINALLY() {
if (ex)
printf("finally: try failed -> %s (%s:%d)\n", ex, ex_file, ex_line);
else
printf("finally: try succeeded\n");
}
ENDTRY

return 0;
}
29 changes: 29 additions & 0 deletions ex-03.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdio.h>
#include "exception.h"

EXCEPTION(division_by_zero);

static int idiv(int a, int b)
{
if (b == 0)
THROW(division_by_zero);

return a/b;
}

int main(void)
{
TRY {
idiv(1,0);
printf("never reached\n");
}
CATCH(bad_alloc) {
printf("catch: exception %s (%s:%d) caught\n", ex, ex_file, ex_line);
}
CATCH_ANY() {
printf("catch_any: exception %s (%s:%d) caught\n", ex, ex_file, ex_line);
}
ENDTRY

return 0;
}
29 changes: 29 additions & 0 deletions ex-04.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdio.h>
#include "exception.h"

EXCEPTION(division_by_zero);

static int idiv(int a, int b)
{
if (b == 0)
THROW(division_by_zero);

return a/b;
}

int main(void)
{
TRY {
idiv(1,0);
printf("never reached\n");
}
CATCH(bad_alloc) {
printf("catch: exception %s (%s:%d) caught\n", ex, ex_file, ex_line);
}
CATCH(division_by_zero) {
printf("catch: exception %s (%s:%d) caught\n", ex, ex_file, ex_line);
}
ENDTRY

return 0;
}
Loading

0 comments on commit d8acd10

Please sign in to comment.