Index: /pjproject/trunk/pjlib/include/pj/except.h
===================================================================
--- /pjproject/trunk/pjlib/include/pj/except.h	(revision 2877)
+++ /pjproject/trunk/pjlib/include/pj/except.h	(revision 2878)
@@ -57,15 +57,4 @@
  * The exception handling mechanism is completely thread safe, so the exception
  * thrown by one thread will not interfere with other thread.
- *
- * CAVEATS:
- *  - unlike C++ exception, the scheme here won't call destructors of local
- *    objects if exception is thrown. Care must be taken when a function
- *    hold some resorce such as pool or mutex etc.
- *  - You CAN NOT make nested exception in one single function without using
- *    a nested PJ_USE_EXCEPTION.
- *  - You can not provide more than PJ_CATCH or PJ_CATCH_ANY nor use PJ_CATCH
- *    and PJ_CATCH_ANY for a single PJ_TRY.
- *  - Exceptions will always be caught by the first handler (unlike C++ where
- *    exception is only caught if the type matches.
  *
  * The exception handling constructs are similar to C++. The blocks will be
@@ -128,4 +117,64 @@
  * ID is raised by default pool policy when it fails to allocate memory.
  *
+ * CAVEATS:
+ *  - unlike C++ exception, the scheme here won't call destructors of local
+ *    objects if exception is thrown. Care must be taken when a function
+ *    hold some resorce such as pool or mutex etc.
+ *  - You CAN NOT make nested exception in one single function without using
+ *    a nested PJ_USE_EXCEPTION. Samples:
+  \verbatim
+	void wrong_sample()
+	{
+	    PJ_USE_EXCEPTION;
+
+	    PJ_TRY {
+		// Do stuffs
+		...
+	    }
+	    PJ_CATCH_ANY {
+		// Do other stuffs
+		....
+		..
+
+		// The following block is WRONG! You MUST declare 
+		// PJ_USE_EXCEPTION once again in this block.
+		PJ_TRY {
+		    ..
+		}
+		PJ_CATCH_ANY {
+		    ..
+		}
+		PJ_END;
+	    }
+	    PJ_END;
+	}
+
+  \endverbatim
+
+ *  - You MUST NOT exit the function inside the PJ_TRY block. The correct way
+ *    is to return from the function after PJ_END block is executed. 
+ *    For example, the following code will yield crash not in this code,
+ *    but rather in the subsequent execution of PJ_TRY block:
+  \verbatim
+        void wrong_sample()
+	{
+	    PJ_USE_EXCEPTION;
+
+	    PJ_TRY {
+		// do some stuffs
+		...
+		return;	        <======= DO NOT DO THIS!
+	    }
+	    PJ_CATCH_ANY {
+	    }
+	    PJ_END;
+	}
+  \endverbatim
+  
+ *  - You can not provide more than PJ_CATCH or PJ_CATCH_ANY nor use PJ_CATCH
+ *    and PJ_CATCH_ANY for a single PJ_TRY.
+ *  - Exceptions will always be caught by the first handler (unlike C++ where
+ *    exception is only caught if the type matches.
+
  * \section PJ_EX_KEYWORDS Keywords
  *
@@ -311,5 +360,5 @@
  * Pop exception handler.
  */
-PJ_DECL(void) pj_pop_exception_handler_(void);
+PJ_DECL(void) pj_pop_exception_handler_(struct pj_exception_state_t *rec);
 
 /**
@@ -344,5 +393,5 @@
  * @hideinitializer
  */
-#define PJ_END			pj_pop_exception_handler_(); \
+#define PJ_END			pj_pop_exception_handler_(&pj_x_except__); \
 			    } else {}
 
Index: /pjproject/trunk/pjlib/src/pj/except.c
===================================================================
--- /pjproject/trunk/pjlib/src/pj/except.c	(revision 2877)
+++ /pjproject/trunk/pjlib/src/pj/except.c	(revision 2878)
@@ -51,4 +51,5 @@
         /* This will crash the system! */
     }
+    pj_pop_exception_handler_(handler);
     pj_longjmp(handler->state, exception_id);
 }
@@ -87,5 +88,5 @@
 }
 
-PJ_DEF(void) pj_pop_exception_handler_(void)
+PJ_DEF(void) pj_pop_exception_handler_(struct pj_exception_state_t *rec)
 {
     struct pj_exception_state_t *handler;
@@ -93,6 +94,7 @@
     handler = (struct pj_exception_state_t *)
 	      pj_thread_local_get(thread_local_id);
-    pj_assert(handler != NULL);
-    pj_thread_local_set(thread_local_id, handler->prev);
+    if (handler && handler==rec) {
+	pj_thread_local_set(thread_local_id, handler->prev);
+    }
 }
 #endif
Index: /pjproject/trunk/pjlib/src/pjlib-test/exception.c
===================================================================
--- /pjproject/trunk/pjlib/src/pjlib-test/exception.c	(revision 2877)
+++ /pjproject/trunk/pjlib/src/pjlib-test/exception.c	(revision 2878)
@@ -62,4 +62,51 @@
     PJ_THROW( ID_2 );
     PJ_UNREACHED(return -1;)
+}
+
+static int try_catch_test(void)
+{
+    PJ_USE_EXCEPTION;
+    int rc = -200;
+
+    PJ_TRY {
+	PJ_THROW(ID_1);
+    }
+    PJ_CATCH_ANY {
+	rc = 0;
+    }
+    PJ_END;
+    return rc;
+}
+
+static int throw_in_handler(void)
+{
+    PJ_USE_EXCEPTION;
+    int rc = 0;
+
+    PJ_TRY {
+	PJ_THROW(ID_1);
+    }
+    PJ_CATCH_ANY {
+	if (PJ_GET_EXCEPTION() != ID_1)
+	    rc = -300;
+	else
+	    PJ_THROW(ID_2);
+    }
+    PJ_END;
+    return rc;
+}
+
+static int return_in_handler(void)
+{
+    PJ_USE_EXCEPTION;
+
+    PJ_TRY {
+	PJ_THROW(ID_1);
+    }
+    PJ_CATCH_ANY {
+	return 0;
+    }
+    PJ_END;
+    return -400;
 }
 
@@ -154,4 +201,59 @@
 	return rc;
 
+    /*
+     * Nested handlers
+     */
+    PJ_TRY {
+	rc = try_catch_test();
+    }
+    PJ_CATCH_ANY {
+	rc = -70;
+    }
+    PJ_END;
+
+    if (rc != 0)
+	return rc;
+
+    /*
+     * Throwing exception inside handler
+     */
+    rc = -80;
+    PJ_TRY {
+	int rc2;
+	rc2 = throw_in_handler();
+	if (rc2)
+	    rc = rc2;
+    }
+    PJ_CATCH_ANY {
+	if (PJ_GET_EXCEPTION() == ID_2) {
+	    rc = 0;
+	} else {
+	    rc = -90;
+	}
+    }
+    PJ_END;
+
+    if (rc != 0)
+	return rc;
+
+
+    /*
+     * Return from handler. Returning from the function inside a handler
+     * should be okay (though returning from the function inside the
+     * PJ_TRY block IS NOT OKAY!!). We want to test to see if handler
+     * is cleaned up properly, but not sure how to do this.
+     */
+    PJ_TRY {
+	int rc2;
+	rc2 = return_in_handler();
+	if (rc2)
+	    rc = rc2;
+    }
+    PJ_CATCH_ANY {
+	rc = -100;
+    }
+    PJ_END;
+
+
     return 0;
 }
