//
// TERM.C -- Sample termination handlers with try-finally
//
#include <stdio.h>
#include <stdlib.h>
#include <pharlap.h>
#include <excpt.h>

void Level2(void);
void Level3(void);

int main()
{
	__try
	{
		printf("Hello from main()\n");
		Level2();
	}
	__finally
	{
		if (AbnormalTermination())
		{
			printf("Abnormal termination in main()\n");
			fflush(stdout);
		}
		else
			printf("Normal termination in main()\n");
	}

	printf("Exiting from main()\n");
	return 0;
}

void Level2(void)
{
	__try
	{
		printf("Hello from Level2()\n");
		Level3();
		printf("Causing exception in Level2() try body\n");
		_asm
		{
			mov	eax,7FFFFFFFh
			mov	ebx,7FFFFFFFh
			add	eax,ebx
			into			; cause integer overflow
		}
	}
	__finally
	{
		if (AbnormalTermination())
			printf("Abnormal termination in Level2()\n");
		else
			printf("Normal termination in Level2()\n");
	}

	printf("Exiting from Level2()\n");
	return;
}

void Level3(void)
{
	__try
	{
		printf("Hello from Level3()\n");
	}
	__finally
	{
		if (AbnormalTermination())
			printf("Abnormal termination in Level3()\n");
		else
			printf("Normal termination in Level3()\n");
	}

	printf("Exiting from Level3()\n");
	return;
}
