Index: src/libcfa/exception.c
===================================================================
--- src/libcfa/exception.c	(revision fcd17b2f5e850f4eee4f611d4b45b099ef8fb923)
+++ src/libcfa/exception.c	(revision 86d5ba7c5d88e75a531a8a77aa1b549a29ed1f55)
@@ -9,7 +9,7 @@
 // Author           : Andrew Beach
 // Created On       : Mon Jun 26 15:13:00 2017
-// Last Modified By : Peter A. Buhr
-// Last Modified On : Wed Jul 26 10:37:51 2017
-// Update Count     : 2
+// Last Modified By : Andrew Beach
+// Last Modified On : Fri Jul 28 12:41:00 2017
+// Update Count     : 3
 //
 
@@ -33,12 +33,26 @@
 
 // Temperary global exception context. Does not work with concurency.
-struct shared_stack_t {
+struct exception_context_t {
     struct __cfaehm__try_resume_node * top_resume;
     struct __cfaehm__try_resume_node * current_resume;
 
-    exception current_exception;
+    exception * current_exception;
     int current_handler_index;
-} shared_stack = {NULL, NULL, 0, 0};
-
+
+	// Storage to avoid using the heap for exceptions.
+	exception built_in_storage;
+} shared_stack = {NULL, NULL, 0, 0, 0};
+
+// Get the current exception context.
+// There can be a single global until multithreading occurs, then each stack
+// needs its own. It will have to be updated to handle that.
+struct exception_context_t * this_exception_context() {
+	return &shared_stack;
+}
+//#define SAVE_EXCEPTION_CONTEXT(to_name)
+//struct exception_context_t * to_name = this_exception_context();
+//exception * this_exception() {
+//    return this_exception_context()->current_exception;
+//}
 
 
@@ -94,8 +108,53 @@
 // TERMINATION ===============================================================
 
-// Requires -fexceptions to work.
-
-// Global which defines the current exception.  Currently an int just to make matching easier.
-//int this_exception; (became shared_stack.current_exception)
+// MEMORY MANAGEMENT (still for integers)
+// May have to move to cfa for constructors and destructors.
+
+// Creates a copy of the indicated exception and sets current_exception to it.
+static void __cfaehm__allocate_exception( exception * except ) {
+	struct exception_context_t * context = this_exception_context();
+
+	// Try to use the context's store, otherwise use the heap.
+	if ( 0 == context->built_in_storage ) {
+		context->current_exception = &context->built_in_storage;
+	} else {
+		exception * new_copy = malloc( sizeof( exception/*int*/ ) );
+		if ( ! new_copy ) {
+			// Failure: cannot allocate exception. Terminate thread.
+			exit(1); // <- thread or program?
+		}
+		context->current_exception = new_copy;
+	}
+
+	// Copy the exception to storage.
+	*context->current_exception = *except;
+}
+
+// Delete the provided exception, unsetting current_exception if relivant.
+static void __cfaehm__delete_exception( exception * except ) {
+	struct exception_context_t * context = this_exception_context();
+
+	// DEBUG
+	printf( "Deleting Exception %d (%s)\n", *except,
+		(&context->built_in_storage == except) ? "builtin" : "dynamic" );
+
+	if ( context->current_exception == except ) {
+		// TODO: This should restore it to the last exception.
+		context->current_exception = NULL;
+	}
+	if ( &context->built_in_storage == except ) {
+		// You can't throw the exception '0'.
+		context->built_in_storage = 0;
+	} else {
+		// Only secondary or too large exceptions are thrown.
+		free( except );
+	}
+}
+
+// If this isn't a rethrow (*except==0), delete the provided exception.
+void __cfaehm__cleanup_terminate( exception ** except ) {
+	if ( *except ) __cfaehm__delete_exception( *except );
+}
+
 
 // We need a piece of storage to raise the exception
@@ -117,10 +176,11 @@
 }
 
-void __cfaehm__throw_terminate( exception * val ) {
-	// Store the current exception
-	shared_stack.current_exception = *val;
-
-	// DEBUG
-	printf("Throwing termination exception %d\n", *val);
+// The exception that is being thrown must already be stored.
+__attribute__((noreturn)) void __cfaehm__begin_unwind(void) {
+	if ( ! this_exception_context()->current_exception ) {
+		printf("UNWIND ERROR missing exception in begin unwind\n");
+		abort();
+	}
+
 
 	// Call stdlibc to raise the exception
@@ -148,10 +208,17 @@
 }
 
-// Nesting this the other way would probably be faster.
+void __cfaehm__throw_terminate( exception * val ) {
+	// DEBUG
+	printf("Throwing termination exception\n");
+
+	__cfaehm__allocate_exception( val );
+	__cfaehm__begin_unwind();
+}
+
 void __cfaehm__rethrow_terminate(void) {
 	// DEBUG
 	printf("Rethrowing termination exception\n");
 
-	__cfaehm__throw_terminate(&shared_stack.current_exception);
+	__cfaehm__begin_unwind();
 }
 
@@ -263,5 +330,5 @@
 					_Unwind_Reason_Code (*matcher)(exception *) =
 						MATCHER_FROM_CONTEXT(context);
-					int index = matcher(&shared_stack.current_exception);
+					int index = matcher(shared_stack.current_exception);
 					_Unwind_Reason_Code ret = (0 == index)
 						? _URC_CONTINUE_UNWIND : _URC_HANDLER_FOUND;
@@ -359,5 +426,5 @@
 	// Exception handler
 	catch_block( shared_stack.current_handler_index,
-	            &shared_stack.current_exception );
+	             shared_stack.current_exception );
 }
 
Index: src/libcfa/exception.h
===================================================================
--- src/libcfa/exception.h	(revision fcd17b2f5e850f4eee4f611d4b45b099ef8fb923)
+++ src/libcfa/exception.h	(revision 86d5ba7c5d88e75a531a8a77aa1b549a29ed1f55)
@@ -9,17 +9,33 @@
 // Author           : Andrew Beach
 // Created On       : Mon Jun 26 15:11:00 2017
-// Last Modified By : Peter A. Buhr
-// Last Modified On : Sat Jul 22 09:57:02 2017
-// Update Count     : 3
+// Last Modified By : Andrew Beach
+// Last Modified On : Fri Jul 27 12:42:00 2017
+// Update Count     : 4
 //
 
 #pragma once
 
-// Later to be a special structure type.
-typedef int exception;
 
 #ifdef __CFORALL__
 extern "C" {
 #endif
+
+#if 1
+typedef int exception;
+#else
+struct exception_t;
+struct exception_t_vtable {
+	struct exception_t_vtable const * parent;
+	size_t size;
+	void (*copy)(struct exception_t *this, struct exception_t * other);
+	void (*free)(struct exception_t *this);
+	const char (*msg)(struct exception_t *this);
+};
+struct exception_t {
+	struct exception_vtable const * virtual_table;
+};
+typedef struct exception_t exception;
+#endif
+
 
 // Used in throw statement translation.
@@ -34,4 +50,7 @@
     int (*match_block)(exception * except));
 
+// Clean-up the exception in catch blocks.
+void __cfaehm__cleanup_terminate(exception ** except);
+
 // Data structure creates a list of resume handlers.
 struct __cfaehm__try_resume_node {
@@ -40,4 +59,5 @@
 };
 
+// These act as constructor and destructor for the resume node.
 void __cfaehm__try_resume_setup(
     struct __cfaehm__try_resume_node * node,
@@ -47,6 +67,5 @@
 
 // Check for a standard way to call fake deconstructors.
-struct __cfaehm__cleanup_hook {
-};
+struct __cfaehm__cleanup_hook {};
 
 #ifdef __CFORALL__
