head	1.6;
access;
symbols
	EX_1_0_6:1.6
	LMTP2NNTP_1_4_1:1.6
	LMTP2NNTP_1_4_0:1.6
	EX_1_0_5:1.6
	LMTP2NNTP_1_3_0:1.6
	LMTP2NNTP_1_3b2:1.6
	LMTP2NNTP_1_3b1:1.6
	LMTP2NNTP_1_3a3:1.6
	LMTP2NNTP_1_3a2:1.6
	LMTP2NNTP_1_3a1:1.6
	EX_1_0_4:1.6
	EX_1_0_3:1.6
	OSSP_RC_0_7_3:1.6
	OSSP_RC_0_7_2:1.6
	OSSP_RC_0_7_1:1.6
	OSSP_RC_ALPHA_06:1.6
	OSSP_RC_EXBROKEN:1.6
	LMTP2NNTP_1_2_0:1.6
	LMTP2NNTP_1_2b4:1.6
	LMTP2NNTP_1_2b3:1.6
	LMTP2NNTP_1_2b2:1.6
	LMTP2NNTP_1_2b1:1.6
	LMTP2NNTP_1_2a8:1.6
	LMTP2NNTP_1_2a7:1.6
	EX_1_0_2:1.6
	EX_1_0_1:1.6
	LMTP2NNTP_1_2a6:1.5
	LMTP2NNTP_1_2a5:1.5
	EX_1_0_0:1.5
	LMTP2NNTP_1_2a4:1.3
	EX_0_9_1:1.3
	LMTP2NNTP_1_2a3:1.1;
locks; strict;
comment	@# @;


1.6
date	2003.01.06.15.31.24;	author rse;	state Exp;
branches;
next	1.5;

1.5
date	2002.03.30.19.04.59;	author rse;	state Exp;
branches;
next	1.4;

1.4
date	2002.03.30.18.55.28;	author rse;	state Exp;
branches;
next	1.3;

1.3
date	2002.03.07.21.30.09;	author rse;	state Exp;
branches;
next	1.2;

1.2
date	2002.03.07.15.01.30;	author rse;	state Exp;
branches;
next	1.1;

1.1
date	2002.02.14.15.56.04;	author rse;	state Exp;
branches;
next	;


desc
@@


1.6
log
@- consistently use "Exception Handling"
- strip trailing whitespaces
- adjust copyright for new year 2003
- consistently use OSSP ASCII-art
@
text
@   _        ___  ____ ____  ____
  |_|_ _   / _ \/ ___/ ___||  _ \   _____  __
  _|_||_| | | | \___ \___ \| |_) | / _ \ \/ /
 |_||_|_| | |_| |___) |__) |  __/ |  __/>  <
  |_|_|_|  \___/|____/____/|_|     \___/_/\_\

  OSSP ex - Exception Handling

  TODO

  -none-

@


1.5
log
@polishing
@
text
@d1 6
a6 5
    _____  __
   / _ \ \/ /
  |  __/>  < 
   \___/_/\_\
               
d10 2
a11 1
  ====
@


1.4
log
@Incorporate Thomas pitfall documentation into ex.pod and
this way remove the last item from OSSP ex' TODO list.
@
text
@d1 10
@


1.3
log
@Implement one of the coolest things since sliced bread: deferred
exceptions. This allows one to more conviniently program the allocation
(and the freeing in case of an error) of multiple (independent)
resources.
@
text
@a0 90

**** IMPROVE DOCUMENTATION ****

/* BAD EXAMPLE */
try {
    char *cp1, *cp2, cp3;

    cp1 = mallocex(SMALLAMOUNT);
    globalcontext->first = cp1;
    cp2 = mallocex(TOOBIG);
    cp3 = mallocex(SMALLAMOUNT);

    strcpy(cp1, "foo"); strcpy(cp2, "bar");
}
clean {
    if (cp3 != NULL) free(cp3);
    if (cp2 != NULL) free(cp2);
    if (cp1 != NULL) free(cp1);
}
catch(ex) {
    printf("cp3=%s", cp3);
    rethrow;
}

The code above shows some pitfalls and contains many errors.

01) variable scope

Variables which are used in the "clean" or "catch" blocks must be declared
before "try", otherwise they only exists inside the "try" block. In the
example above, cp1-3 are automatic variables and only exist in the "try"
block, the "clean" and "catch" blocks don't know anything about them.

02) variable initialization

Variables which are used in the "clean" or "catch" blocks must be initialized
before the point of the first possible "throw" is reached. In the example
above, "clean" would have trouble using cp3 when mallocex() throws a exception
when allocating a TOOBIG buffer.

03) volatile variables

Variables which are used in the "clean" or "catch" blocks must be declared
volatile, otherwise they might contain outdated information when "throw" does
the longjmp(). When using a "free if unset" mechanism like the example does in
the "clean" block, the variables must be initialized (see 02) *and* remain
valid upon use.

04) clean before catch

The "clean" block is evaluated before "catch", so resources being cleaned up
must no longer be used in the "catch" block. The example above would have
trouble referencing the character strings in the printf() statement because
these have been free()d before.

05) variable uninitialization

When resources are passed away and out of the scope of the "try/clean/catch"
construct and the variables were initialized for using a "free if unset"
mechanism then they must be uninitialized after being passed away.  The
example above would free() cp1 in the "clean" block when mallocex() throws a
exception when allocating a TOOBIG buffer. The globalcontext->first pointer
hence becomes invalid.

/* GOOD EXAMPLE */                                       | /* ALTERNATE *05* GOOD EXAMPLE */
{ /*01*/                                                 | { /*01*/
    volatile /*03*/ char *cp1 = NULL /*02*/;             |     volatile /*03*/ char *cp1 = NULL /*02*/;
    volatile /*03*/ char *cp2 = NULL /*02*/;             |     volatile /*03*/ char *cp2 = NULL /*02*/;
    volatile /*03*/ char *cp3 = NULL /*02*/;             |     volatile /*03*/ char *cp3 = NULL /*02*/;
    try {                                                |     try {
        cp1 = mallocex(SMALLAMOUNT);                     |         cp1 = mallocex(SMALLAMOUNT);
        globalcontext->first = cp1;                      |         globalcontext->first = cp1;
        cp1 = NULL /*05 give away*/;                     |         /*05 keep responsibility*/
        cp2 = mallocex(TOOBIG);                          |         cp2 = mallocex(TOOBIG);
        cp3 = mallocex(SMALLAMOUNT);                     |         cp3 = mallocex(SMALLAMOUNT);
                                                         | 
        strcpy(cp1, "foo"); strcpy(cp2, "bar");          |         strcpy(cp1, "foo"); strcpy(cp2, "bar");
    }                                                    |     }
    clean {                                              |     clean {
 /*04*/ printf("cp3=%s", cp3 == NULL /*02*/ ? "" : cp3); |  /*04*/ printf("cp3=%s", cp3 == NULL /*02*/ ? "" : cp3);
        if (cp3 != NULL) free(cp3);                      |         if (cp3 != NULL) free(cp3);
        if (cp2 != NULL) free(cp2);                      |         if (cp2 != NULL) free(cp2);
        /*05 cp1 was given away */                       |         if (cp1 != NULL) free(cp1);
    }                                                    |     }
    catch(ex) {                                          |     catch(ex) {
        /*05 global context untouched */                 |         globalcontext->first = NULL;
         rethrow;   }                                    |         rethrow;
    }                                                    |     }
}                                                        | }

@


1.2
log
@add Thomas' idea
@
text
@a1 25
Idea: deferred exceptions:
o ex_defer BLOCK
o if (ex_deferred) ...

to avoid having to initialize every variable in this context:

char *cp1;
char *cp2;
char *cp3;
ex_try {
    ex_defer {
        cp1 = mymalloc(...);
        cp2 = mymalloc(...);
        cp3 = mymalloc(...);
    }
}
ex_cleanup {
    if (cp1 != NULL)
        free(cp1);
    if (cp2 != NULL)
        free(cp2);
    if (cp3 != NULL)
        free(cp3);
}

@


1.1
log
@don't forget the lessons learned today
@
text
@d2 25
@

