lkml.org 
[lkml]   [1997]   [Sep]   [13]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: patch for 2.1.55 pre-1 minix/sysv/affs

On Fri, 12 Sep 1997, Linus Torvalds wrote:
> Sure. We'd also be about 500 times slower.
>
> Exception handling is _complex_. That translates into slow.

Ok, I agree the full fledged, regular OO exception handling (where
exceptions involve allocating "objects" and passing them up hierarchies to
find the right handlers) can be slow.

However, for the quick and dirty usage of exceptions (the most used I
think), Ive come up with these simple C macro's. Would anyone care to
check them out?

I believe they involve almost no performance hit over the conventional
nested if()'s......

To use the code, just #include "exceptions.h"

--- exceptions.h ---

/* exceptions.h -- Macros to emulate exception handling in C
*
* September 11, 1997
* by Jonathan Walther jwalther@citytel.net
*
* Datatypes:
* an "exception" is actually an int.
* each thread gets one "int" dedicated to hold exception values called
* "e"
* Mappings between an integer value and what it "means" when thrown as
* an exception can vary between try{} statements. So that the ints
* you throw make sense, use #define's to give the int a meaning.
*
* Usage:
*
* #define FOO_ERR 3
*
* try
* // code
* if (foo) throw(FOO_ERR);
* // more code
* endtry
* catch(FOO_ERR)
* // handle it
* catch(BAR_ERR)
* // if it had been thrown, this would handle it
* finally
* // do this no matter what
* endcatching
*
* try, endtry, and endcatching are mandatory. The other clauses may be
* omitted.
*
* in the catch and finally clauses, more than one statement must be put
* in braces. eg,
*
* catch(FOO_ERR) blah(); endcatching // correct
* catch(FOO_ERR) blah(); bing(); endcatching // not correct
* catch(FOO_ERR) { blah(); bing(); } endcatching // correct
*
* Final note: Whenever you invoke "try", e is modified. If you wish to
* preserve the value, stash it in another variable.
*/

#define try e = 0; do {
#define throw(x) { e = x; break; }
#define endtry } while(0); if (e){ if (0) {}
#define catch(x) else if ( e == x )
#define finally } {
#define endcatching }

int e;

--- end of exceptions.h ---

/* exceptions.h -- Macros to emulate exception handling in C
*
* September 11, 1997
* by Jonathan Walther jwalther@citytel.net
*
* Datatypes:
* an "exception" is actually an int.
* each thread gets one "int" dedicated to holding exception values called "e"
* Mappings between an integer value and what it "means" when thrown as an
* exception can vary between try{} statements. So that the ints you throw
* make sense, use #define's to give the int a meaning.
*
* Usage:
*
* #define FOO_ERR 3
*
* try
* // code
* if (foo) throw(FOO_ERR);
* // more code
* endtry
* catch(FOO_ERR)
* // handle it
* finally
* // do this no matter what
* endcatching
*
* try, endtry, and endcatching are mandatory. The other clauses may be
* omitted.
*
* in the catch and finally clauses, more than one statement must be put in
* braces. eg,
*
* catch(FOO_ERR) blah(); endcatching // correct
* catch(FOO_ERR) blah(); bing(); endcatching // not correct
* catch(FOO_ERR) { blah(); bing(); } endcatching // correct
*
* Final note: Whenever you invoke "try", e is modified. If you wish to
* preserve the value, stash it in another variable.
*/

#define try e = 0; do {
#define throw(x) { e = x; break; }
#define endtry } while(0); if (e){ if (0) {}
#define catch(x) else if ( e == x )
#define finally } {
#define endcatching }

int e;

\
 
 \ /
  Last update: 2005-03-22 13:40    [W:0.061 / U:0.056 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site