/* 
   exceptions.m

   Copyright (C) 1995, 1996, 1997 Ovidiu Predescu and Mircea Oancea.
   All rights reserved.

   Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>

   This file is part of libFoundation.

   Permission to use, copy, modify, and distribute this software and its
   documentation for any purpose and without fee is hereby granted, provided
   that the above copyright notice appear in all copies and that both that
   copyright notice and this permission notice appear in supporting
   documentation.

   We disclaim all warranties with regard to this software, including all
   implied warranties of merchantability and fitness, in no event shall
   we be liable for any special, indirect or consequential damages or any
   damages whatsoever resulting from loss of use, data or profits, whether in
   an action of contract, negligence or other tortious action, arising out of
   or in connection with the use or performance of this software.
*/

#include <config.h>

#include <Foundation/NSString.h>
#include <Foundation/NSUtilities.h>

#if NeXT_foundation_LIBRARY || GNUSTEP_BASE_LIBRARY || LIBOBJECTS_LIBRARY
# include <Foundation/NSObjCRuntime.h>
#endif

#include <extensions/NSException.h>

#include "ExceptionClasses.h"
#include "tests.h"

void try_catch1(void)
{
    volatile int ret;

    puts(__FUNCTION__);
    TRY {
	action;
	if(ret == EXCEPTION_BREAK)
	    BREAK;
    } END_TRY
    CATCH(ExceptionClass1) {
	puts("caught ExceptionClass1 in try_catch1");
	action;
    } END_CATCH
    puts("Return from " __FUNCTION__);
}

void try_catch2(void)
{
    volatile int ret;

    puts(__FUNCTION__);

    TRY {
	action;
	if(ret == EXCEPTION_BREAK)
	    BREAK;
    } END_TRY
    CATCH(ExceptionClass2) {
	puts("caught ExceptionClass2 in try_catch2");
	action;
    } END_CATCH
    puts("Return from " __FUNCTION__);
}

void try_multicatch(void)
{
    volatile int ret;

    puts(__FUNCTION__);
    TRY {
	action;
	if(ret == EXCEPTION_BREAK)
	    BREAK;
    } END_TRY
    MULTICATCH([ExceptionClass1 class], [ExceptionClass2 class]) {
	puts("caught exception in try_multicatch");
	action;
    } END_CATCH
    puts("Return from " __FUNCTION__);
}

void nsduring_nshandler(void)
{
    volatile int ret;

    puts(__FUNCTION__);
    NS_DURING
	action;
    NS_HANDLER
	printf("caught exception %s in nsduring_nshandler with reason:\n%s\n",
		    [NSStringFromClass([localException class]) cString],
		    [[localException errorString] cString]);
	action;
    NS_ENDHANDLER
    puts("Return from " __FUNCTION__);
}

#ifndef NO_NESTED_FUNCTIONS
void try_cleanup_catch1(void)
{
    volatile int ret;

    puts(__FUNCTION__);
    TRY {
	action;
	if(ret == EXCEPTION_BREAK)
	    BREAK;
    } END_TRY
    CATCH(ExceptionClass1) {
	puts("caught ExceptionClass1 in try_cleanup_catch1");
	action;
    }
    CLEANUP {
	puts("cleanup in try_cleanup_catch1");
    }
    END_CATCH
    puts("Return from " __FUNCTION__);
}

void try_cleanup_catch2(void)
{
    volatile int ret;

    puts(__FUNCTION__);
    TRY {
	action;
	if(ret == EXCEPTION_BREAK)
	    BREAK;
    } END_TRY
    CATCH(ExceptionClass2) {
	puts("caught ExceptionClass2 in try_cleanup_catch2");
	action;
    }
    CLEANUP {
	puts("cleanup in try_cleanup_catch2");
	action;
    }
    END_CATCH
    puts("Return from " __FUNCTION__);
}

void try_finally_catch1(void)
{
    volatile int ret;

    puts(__FUNCTION__);
    TRY {
	action;
	if(ret == EXCEPTION_BREAK)
	    BREAK;
    } END_TRY
    CATCH(ExceptionClass1) {
	puts("caught ExceptionClass1 in try_finally_catch1");
	action;
    }
    FINALLY {
	puts("finally in try_finally_catch1");
    }
    END_CATCH
    puts("Return from " __FUNCTION__);
}

void try_finally_catch2(void)
{
    volatile int ret;

    puts(__FUNCTION__);
    TRY {
	action;
	if(ret == EXCEPTION_BREAK)
	    BREAK;
    } END_TRY
    CATCH(ExceptionClass2) {
	puts("caught ExceptionClass2 in try_finally_catch2");
	action;
    }
    FINALLY {
	puts("finally in try_finally_catch2");
	action;
    }
    END_CATCH
    puts("Return from " __FUNCTION__);
}

void try_cleanup_nocatch(void)
{
    volatile int ret;

    puts(__FUNCTION__);
    TRY {
	action;
	if(ret == EXCEPTION_BREAK)
	    BREAK;
    } END_TRY
    CLEANUP {
	puts("cleanup in try_cleanup_nocatch");
	action;
    }
    END_CATCH
    puts("Return from " __FUNCTION__);
}

void try_finally_nocatch(void)
{
    volatile int ret;

    puts(__FUNCTION__);
    TRY {
	action;
	if(ret == EXCEPTION_BREAK)
	    BREAK;
    } END_TRY
    FINALLY {
	puts("finally in try_finally_nocatch");
	action;
    }
    END_CATCH
    puts("Return from " __FUNCTION__);
}

void try_cleanup_finally_nocatch(void)
{
    volatile int ret;

    puts(__FUNCTION__);
    TRY {
	action;
	if(ret == EXCEPTION_BREAK)
	    BREAK;
    } END_TRY
    CLEANUP {
	puts("cleanup in try_cleanup_finally_nocatch");
	action;
    }
    FINALLY {
	puts("finally in try_cleanup_finally_nocatch");
	action;
    }
    END_CATCH
    puts("Return from " __FUNCTION__);
}

void try_cleanup_cleanup_finally_nocatch(void)
{
    volatile int ret;

    puts(__FUNCTION__);
    TRY {
	action;
	if(ret == EXCEPTION_BREAK)
	    BREAK;
    } END_TRY
    CLEANUP {
	puts("cleanup1 in try_cleanup_cleanup_finally_nocatch");
	action;
    }
    CLEANUP {
	puts("cleanup2 in try_cleanup_cleanup_finally_nocatch");
	action;
    }
    FINALLY {
	puts("finally in try_cleanup_cleanup_finally_nocatch");
	action;
    }
    END_CATCH
    puts("Return from " __FUNCTION__);
}
#endif /* NO_NESTED_FUNCTIONS */
