#include <stdio.h>

#include "Desk.Error2.h"
#include "Desk.DeskMem.h"
#include "Desk.Debug.h"


static void	Foo( void)
	{
	Desk_Error2_TryCatch(
		Desk_Error2_HandleText( Desk_error_PLACE "Example text error");
		,
		Desk_Error2_Describe( stdout, &Desk_Error2_globalblock);
		printf( "\n");
		)
	return;
	}


static void	Bar( void)
/*
This is an example of how to clean up after errors...
 */
	{
	void* volatile	p1 = NULL;
	void* volatile	p2 = NULL;
	/* 
	These are volatile because non-volatile automatic 
	variables have undefined values after a longjmp.
	 */
	
	Desk_Error2_Try	{
		p1 = Desk_DeskMem_Malloc( 32);
		p2 = Desk_DeskMem_Malloc( 99999999);
		/*
		Use p1 and p2, call any functions 
		which could raise an error etc
		 */
		}
	Desk_Error2_Catch	{
		/*
		If we get here, an error has occurred 
		in the 'Try' block above.
		 */
		/*
		If p1 and p2 weren't volatile, they 
		would have undefined values here.
		 */
		Desk_DeskMem_Free( p1);
		Desk_DeskMem_Free( p2);
		Desk_Error2_ReThrow();
		}
	Desk_Error2_EndCatch
	
	Desk_DeskMem_Free( p1);
	Desk_DeskMem_Free( p2);
	}




int	main( void)
{
Desk_Error2_Init_JumpSig();
Desk_Debug_SetNestingIndentation( "  ");
Desk_Debug_Printf( "Started...\n");

Desk_Error2_Try	{
	Foo();
	Desk_DeskMem_Malloc( 100000000);	/* Should cause an error...	*/
	}
Desk_Error2_Catch	{
	printf( Desk_error_PLACE "An error occurred: ");
	Desk_Error2_Describe( stdout, &Desk_Error2_globalblock);
	printf( "\n");
	}
Desk_Error2_EndCatch

Desk_Error2_TryCatch(
	Bar();
	,
	printf( Desk_error_PLACE "An error occurred from Bar(): ");
	Desk_Error2_Describe( stdout, &Desk_Error2_globalblock);
	printf( "\n");
	)

printf( "Hello world\n");

return 0;
}
