/* $Id: Exception,v 1.6 2009/02/28 18:37:30 kiesling Exp $ -*-c-mode-*-*/

/*
  This file is part of Ctalk.
  Copyright  2005-2009  Robert Kiesling, ctalk@ctalklang.org.
  Permission is granted to copy this software provided that this copyright
  notice is included in all source code modules.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*/

/*
 *  Exception Class
 */

Event class Exception;

/*
 *  trap (void)
 *   Wrapper method for __ctalkTrapExceptionInternal ().
 *   When called at run time, the argument to the library
 *   function must be NULL, and it will return TRUE if
 *   there are any pending exceptions.
 */
Exception instanceMethod pending (void) {
  char buf[MAXLABEL];
  int r;

  returnObjectClass Integer;

  r = __ctalkPendingException ();
  __ctalkDecimalIntegerToASCII (((r) ? 1 : 0), buf);
  return __ctalkCreateObjectInit ("result", "Integer", "Magnitude",
 				  LOCAL_VAR, buf);
}

/*
 *  handle (void)
 *   Handle a pending exception. __ctalkHandleRunTimeException ()
 *   always returns 0, and leaves it up to the exception handler
 *   how to cope with the exception.
 */

Exception instanceMethod handle (void) {

  returnObjectClass Integer;
  if (self traceEnabled) {
    self printExceptionTrace;
  }
  __ctalkHandleRunTimeException ();
  return __ctalkCreateObjectInit ("result", "Integer", "Magnitude",
 				  LOCAL_VAR, "0");
}

Exception instanceMethod printExceptionTrace (void) {
  __ctalkPrintExceptionTrace ();
  methodReturnNULL;
}

Exception instanceMethod raiseException (int errcode, char *data) {
  __ctalkExceptionInternal (NULL, (EXCEPTION)errcode, data);
  methodReturnNULL;
}

Exception instanceMethod raiseCriticalException (int errcode, char *data) {
  __ctalkCriticalExceptionInternal (NULL, (EXCEPTION)errcode, data);
  methodReturnNULL;
}

