2001-12-17 14:31:42 +00:00
|
|
|
/** NSException - Object encapsulation of a general exception handler
|
1999-04-21 20:16:25 +00:00
|
|
|
Copyright (C) 1993, 1994, 1996, 1997, 1999 Free Software Foundation, Inc.
|
1995-03-18 17:15:15 +00:00
|
|
|
|
|
|
|
Written by: Adam Fedor <fedor@boulder.colorado.edu>
|
|
|
|
Date: Mar 1995
|
|
|
|
|
1996-05-12 00:56:10 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
1995-03-18 17:15:15 +00:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
1999-04-21 20:16:25 +00:00
|
|
|
|
1995-03-18 17:15:15 +00:00
|
|
|
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
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this library; if not, write to the Free
|
1999-09-09 02:56:20 +00:00
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
|
2001-12-18 16:54:15 +00:00
|
|
|
|
|
|
|
<title>NSException class reference</title>
|
|
|
|
$Date$ $Revision$
|
1995-03-18 17:15:15 +00:00
|
|
|
*/
|
1996-05-28 18:27:40 +00:00
|
|
|
|
1997-11-06 00:51:23 +00:00
|
|
|
#include <config.h>
|
1998-12-20 21:27:47 +00:00
|
|
|
#include <base/preface.h>
|
1995-04-17 21:13:20 +00:00
|
|
|
#include <Foundation/NSException.h>
|
|
|
|
#include <Foundation/NSString.h>
|
|
|
|
#include <Foundation/NSArray.h>
|
|
|
|
#include <Foundation/NSCoder.h>
|
1997-03-03 19:51:04 +00:00
|
|
|
#include <Foundation/NSThread.h>
|
1995-04-17 21:13:20 +00:00
|
|
|
#include <Foundation/NSDictionary.h>
|
2001-05-31 22:39:16 +00:00
|
|
|
#include <stdio.h>
|
2002-03-27 09:55:57 +00:00
|
|
|
|
2002-06-09 11:36:13 +00:00
|
|
|
/**
|
|
|
|
* A generic exception for general purpose usage.
|
|
|
|
*/
|
2002-06-09 11:47:45 +00:00
|
|
|
NSString* const NSGenericException
|
2002-06-09 11:36:13 +00:00
|
|
|
= @"NSGenericException";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An exception for caes where unexpected state is detected within an object
|
|
|
|
*/
|
2002-06-09 11:47:45 +00:00
|
|
|
NSString* const NSInternalInconsistencyException
|
2002-06-09 11:36:13 +00:00
|
|
|
= @"NSInternalInconsistencyException";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An exception used when an invalid argument is passed to a method
|
|
|
|
* or function.
|
|
|
|
*/
|
2002-06-09 11:47:45 +00:00
|
|
|
NSString* const NSInvalidArgumentException
|
2002-06-09 11:36:13 +00:00
|
|
|
= @"NSInvalidArgumentException";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An exception used when the system faols to allocate required memory.
|
|
|
|
*/
|
2002-06-09 11:47:45 +00:00
|
|
|
NSString* const NSMallocException
|
2002-06-09 11:36:13 +00:00
|
|
|
= @"NSMallocException";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An exception used when an illegal range is encountered ... usually this
|
|
|
|
* is used to provide more information than an invalid argument exception.
|
|
|
|
*/
|
2002-06-09 11:47:45 +00:00
|
|
|
NSString* const NSRangeException
|
2002-06-09 11:36:13 +00:00
|
|
|
= @"NSRangeException";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An exception when character set conversion fails.
|
|
|
|
*/
|
2002-06-09 11:47:45 +00:00
|
|
|
NSString* const NSCharacterConversionException
|
2002-06-09 11:36:13 +00:00
|
|
|
= @"NSCharacterConversionException";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An exception used when some form of parsing fails.
|
|
|
|
*/
|
2002-06-09 11:47:45 +00:00
|
|
|
NSString* const NSParseErrorException
|
2002-06-09 11:36:13 +00:00
|
|
|
= @"NSParseErrorException";
|
|
|
|
|
2002-03-27 09:55:57 +00:00
|
|
|
#include "GSPrivate.h"
|
2001-01-25 08:05:52 +00:00
|
|
|
|
2001-01-08 20:02:09 +00:00
|
|
|
static void
|
|
|
|
_preventRecursion (NSException *exception)
|
|
|
|
{
|
2002-04-16 13:48:14 +00:00
|
|
|
fprintf(stderr, "recursion encountered handling uncaught exception\n");
|
|
|
|
fflush(stderr); /* NEEDED UNDER MINGW */
|
2001-01-08 20:02:09 +00:00
|
|
|
}
|
|
|
|
|
2000-04-05 14:21:05 +00:00
|
|
|
static void
|
|
|
|
_NSFoundationUncaughtExceptionHandler (NSException *exception)
|
1995-03-18 17:15:15 +00:00
|
|
|
{
|
2002-04-16 15:09:00 +00:00
|
|
|
BOOL a;
|
|
|
|
extern const char* GSArgZero();
|
2001-01-25 08:05:52 +00:00
|
|
|
|
2001-01-08 20:02:09 +00:00
|
|
|
_NSUncaughtExceptionHandler = _preventRecursion;
|
2002-04-16 15:09:00 +00:00
|
|
|
#if 1
|
|
|
|
fprintf(stderr, "%s: Uncaught exception %s, reason: %s\n", GSArgZero(),
|
|
|
|
[[exception name] lossyCString], [[exception reason] lossyCString]);
|
2002-03-25 10:54:59 +00:00
|
|
|
fflush(stderr); /* NEEDED UNDER MINGW */
|
2002-04-16 13:48:14 +00:00
|
|
|
#else
|
|
|
|
NSLog("Uncaught exception %@, reason: %@",
|
|
|
|
[exception name], [exception reason]);
|
|
|
|
#endif
|
2001-01-25 08:05:52 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
a = YES; // abort() by default.
|
|
|
|
#else
|
|
|
|
a = NO; // exit() by default.
|
|
|
|
#endif
|
2002-03-27 09:55:57 +00:00
|
|
|
a = GSEnvironmentFlag("CRASH_ON_ABORT", a);
|
2001-01-25 08:05:52 +00:00
|
|
|
if (a == YES)
|
|
|
|
{
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
exit(1);
|
|
|
|
}
|
1995-03-18 17:15:15 +00:00
|
|
|
}
|
|
|
|
|
2002-10-27 01:38:30 +00:00
|
|
|
/**
|
|
|
|
<p>
|
|
|
|
The NSException class helps manage errors in a program. It provides
|
|
|
|
a mechanism for lower-level methods to provide information about
|
|
|
|
problems to higher-level methods, which more often than not, have a
|
|
|
|
better ability to decide what to do about the problems.
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
Exceptions are typically handled by enclosing a sensitive section
|
|
|
|
of code inside the macros NS_DURING and NS_HANDLER, and then
|
|
|
|
handling any problems after this, up to the NS_ENDHANDLER macro:
|
|
|
|
</p>
|
|
|
|
<example>
|
|
|
|
NS_DURING
|
|
|
|
code that might cause an exception
|
|
|
|
NS_HANDLER
|
|
|
|
code that deals with the exception. If this code cannot deal with
|
|
|
|
it, you can re-raise the exception like this
|
|
|
|
[localException raise]
|
|
|
|
so the next higher level of code can handle it
|
|
|
|
NS_ENDHANDLER
|
|
|
|
</example>
|
|
|
|
<p>
|
|
|
|
The local variable localException is the name of the exception
|
|
|
|
object you can use in the NS_HANDLER section.
|
|
|
|
The easiest way to cause an exeption is using the +raise:format:
|
|
|
|
method.
|
|
|
|
</p>
|
|
|
|
*/
|
1995-03-18 17:15:15 +00:00
|
|
|
@implementation NSException
|
|
|
|
|
2002-10-27 01:38:30 +00:00
|
|
|
/**
|
|
|
|
Create an an exception object with a name, reason and a dictionary
|
|
|
|
userInfo which can be used to provide additional information or
|
|
|
|
access to objects needed to handle the exception. After the
|
|
|
|
exception is created you must -raise it.
|
|
|
|
*/
|
2000-04-05 14:21:05 +00:00
|
|
|
+ (NSException*) exceptionWithName: (NSString*)name
|
|
|
|
reason: (NSString*)reason
|
|
|
|
userInfo: (NSDictionary*)userInfo
|
1995-03-18 17:15:15 +00:00
|
|
|
{
|
1999-04-21 20:16:25 +00:00
|
|
|
return AUTORELEASE([[self alloc] initWithName: name reason: reason
|
2002-01-03 10:01:25 +00:00
|
|
|
userInfo: userInfo]);
|
1995-03-18 17:15:15 +00:00
|
|
|
}
|
|
|
|
|
2002-10-27 01:38:30 +00:00
|
|
|
/**
|
|
|
|
Creates an exception with a name and a reason using the
|
|
|
|
format string and any additional arguments. The exception is then
|
|
|
|
raised.
|
|
|
|
*/
|
2000-04-05 14:21:05 +00:00
|
|
|
+ (void) raise: (NSString*)name
|
|
|
|
format: (NSString*)format,...
|
1995-03-18 17:15:15 +00:00
|
|
|
{
|
1999-04-21 20:16:25 +00:00
|
|
|
va_list args;
|
1995-03-18 17:15:15 +00:00
|
|
|
|
1999-04-21 20:16:25 +00:00
|
|
|
va_start(args, format);
|
|
|
|
[self raise: name format: format arguments: args];
|
2002-10-27 01:38:30 +00:00
|
|
|
// This probably doesn't matter, but va_end won't get called
|
1999-04-21 20:16:25 +00:00
|
|
|
va_end(args);
|
1995-03-18 17:15:15 +00:00
|
|
|
}
|
|
|
|
|
2002-10-27 01:38:30 +00:00
|
|
|
/**
|
|
|
|
Creates an exception with a name and a reason string using the
|
|
|
|
format string and additional arguments specified as a variable
|
|
|
|
argument list argList. The exception is then raised.
|
|
|
|
*/
|
2000-04-05 14:21:05 +00:00
|
|
|
+ (void) raise: (NSString*)name
|
|
|
|
format: (NSString*)format
|
|
|
|
arguments: (va_list)argList
|
1995-03-18 17:15:15 +00:00
|
|
|
{
|
1999-04-21 20:16:25 +00:00
|
|
|
NSString *reason;
|
|
|
|
NSException *except;
|
|
|
|
|
|
|
|
reason = [NSString stringWithFormat: format arguments: argList];
|
|
|
|
except = [self exceptionWithName: name reason: reason userInfo: nil];
|
|
|
|
[except raise];
|
1995-03-18 17:15:15 +00:00
|
|
|
}
|
|
|
|
|
2002-10-27 01:38:30 +00:00
|
|
|
/**
|
|
|
|
<init/>Initializes a newly allocated NSException object with a
|
|
|
|
name, reason and a dictionary userInfo.
|
|
|
|
*/
|
2000-04-05 14:21:05 +00:00
|
|
|
- (id) initWithName: (NSString*)name
|
|
|
|
reason: (NSString*)reason
|
|
|
|
userInfo: (NSDictionary*)userInfo
|
1995-03-18 17:15:15 +00:00
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
ASSIGN(_e_name, name);
|
|
|
|
ASSIGN(_e_reason, reason);
|
|
|
|
ASSIGN(_e_info, userInfo);
|
1999-04-21 20:16:25 +00:00
|
|
|
return self;
|
1995-03-18 17:15:15 +00:00
|
|
|
}
|
|
|
|
|
2000-04-05 14:21:05 +00:00
|
|
|
- (void) dealloc
|
1997-09-13 17:52:31 +00:00
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
DESTROY(_e_name);
|
|
|
|
DESTROY(_e_reason);
|
|
|
|
DESTROY(_e_info);
|
1997-09-13 17:52:31 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2002-10-27 01:38:30 +00:00
|
|
|
/**
|
|
|
|
Raises the exception. All code following the raise will not be
|
|
|
|
executed and program control will be transfered to the closest
|
|
|
|
calling method which encapsulates the exception code in an
|
|
|
|
NS_DURING macro, or to the uncaught exception handler if there is no
|
|
|
|
other handling code.
|
|
|
|
*/
|
2000-04-05 14:21:05 +00:00
|
|
|
- (void) raise
|
1995-03-18 17:15:15 +00:00
|
|
|
{
|
1999-04-21 20:16:25 +00:00
|
|
|
NSThread *thread;
|
|
|
|
NSHandler *handler;
|
1995-03-18 17:15:15 +00:00
|
|
|
|
1999-04-21 20:16:25 +00:00
|
|
|
if (_NSUncaughtExceptionHandler == NULL)
|
|
|
|
{
|
|
|
|
_NSUncaughtExceptionHandler = _NSFoundationUncaughtExceptionHandler;
|
1995-03-18 17:15:15 +00:00
|
|
|
}
|
1997-03-03 19:51:04 +00:00
|
|
|
|
1999-04-21 20:16:25 +00:00
|
|
|
thread = GSCurrentThread();
|
|
|
|
handler = thread->_exception_handler;
|
|
|
|
if (handler == NULL)
|
|
|
|
{
|
|
|
|
_NSUncaughtExceptionHandler(self);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
thread->_exception_handler = handler->next;
|
|
|
|
handler->exception = self;
|
|
|
|
longjmp(handler->jumpState, 1);
|
1995-03-18 17:15:15 +00:00
|
|
|
}
|
|
|
|
|
2002-10-27 01:38:30 +00:00
|
|
|
/** Returns the name of the exception */
|
2000-04-05 14:21:05 +00:00
|
|
|
- (NSString*) name
|
1995-03-18 17:15:15 +00:00
|
|
|
{
|
2000-09-07 16:53:14 +00:00
|
|
|
if (_e_name != nil)
|
|
|
|
{
|
|
|
|
return _e_name;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NSStringFromClass([self class]);
|
|
|
|
}
|
1995-03-18 17:15:15 +00:00
|
|
|
}
|
|
|
|
|
2002-10-27 01:38:30 +00:00
|
|
|
/** Returns the exception reason */
|
2000-04-05 14:21:05 +00:00
|
|
|
- (NSString*) reason
|
1995-03-18 17:15:15 +00:00
|
|
|
{
|
2000-09-07 16:53:14 +00:00
|
|
|
if (_e_reason != nil)
|
|
|
|
{
|
|
|
|
return _e_reason;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return @"unspecified reason";
|
|
|
|
}
|
1995-03-18 17:15:15 +00:00
|
|
|
}
|
|
|
|
|
2002-10-27 01:38:30 +00:00
|
|
|
/** Returns the exception userInfo dictionary */
|
2000-04-05 14:21:05 +00:00
|
|
|
- (NSDictionary*) userInfo
|
1995-03-18 17:15:15 +00:00
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
return _e_info;
|
1995-03-18 17:15:15 +00:00
|
|
|
}
|
|
|
|
|
1998-10-17 18:56:49 +00:00
|
|
|
- (Class) classForPortCoder
|
|
|
|
{
|
|
|
|
return [self class];
|
|
|
|
}
|
|
|
|
|
1999-04-21 20:16:25 +00:00
|
|
|
- (id) replacementObjectForPortCoder: (NSPortCoder*)aCoder
|
1998-10-17 18:56:49 +00:00
|
|
|
{
|
1999-04-21 20:16:25 +00:00
|
|
|
return self;
|
1998-10-17 18:56:49 +00:00
|
|
|
}
|
1995-03-18 17:15:15 +00:00
|
|
|
|
1999-04-21 20:16:25 +00:00
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
1995-03-18 17:15:15 +00:00
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
[aCoder encodeValueOfObjCType: @encode(id) at: &_e_name];
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(id) at: &_e_reason];
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(id) at: &_e_info];
|
1995-03-18 17:15:15 +00:00
|
|
|
}
|
|
|
|
|
1999-04-21 20:16:25 +00:00
|
|
|
- (id) initWithCoder: (NSCoder*)aDecoder
|
1995-03-18 17:15:15 +00:00
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
[aDecoder decodeValueOfObjCType: @encode(id) at: &_e_name];
|
|
|
|
[aDecoder decodeValueOfObjCType: @encode(id) at: &_e_reason];
|
|
|
|
[aDecoder decodeValueOfObjCType: @encode(id) at: &_e_info];
|
1999-04-21 20:16:25 +00:00
|
|
|
return self;
|
1995-03-18 17:15:15 +00:00
|
|
|
}
|
|
|
|
|
1999-04-21 20:16:25 +00:00
|
|
|
- (id) deepen
|
1995-03-18 17:15:15 +00:00
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
_e_name = [_e_name copyWithZone: [self zone]];
|
|
|
|
_e_reason = [_e_reason copyWithZone: [self zone]];
|
|
|
|
_e_info = [_e_info copyWithZone: [self zone]];
|
1999-04-21 20:16:25 +00:00
|
|
|
return self;
|
1995-03-18 17:15:15 +00:00
|
|
|
}
|
|
|
|
|
2000-04-05 14:21:05 +00:00
|
|
|
- (id) copyWithZone: (NSZone*)zone
|
1995-03-18 17:15:15 +00:00
|
|
|
{
|
1999-04-21 20:16:25 +00:00
|
|
|
if (NSShouldRetainWithZone(self, zone))
|
|
|
|
return RETAIN(self);
|
|
|
|
else
|
|
|
|
return [(NSException*)NSCopyObject(self, 0, zone) deepen];
|
1995-03-18 17:15:15 +00:00
|
|
|
}
|
|
|
|
|
1999-04-21 20:16:25 +00:00
|
|
|
- (NSString*) description
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
if (_e_info)
|
1999-05-11 09:43:04 +00:00
|
|
|
return [NSString stringWithFormat: @"%@ NAME:%@ REASON:%@ INFO:%@",
|
1999-09-16 07:21:34 +00:00
|
|
|
[super description], _e_name, _e_reason, _e_info];
|
1999-04-21 20:16:25 +00:00
|
|
|
else
|
1999-05-11 09:43:04 +00:00
|
|
|
return [NSString stringWithFormat: @"%@ NAME:%@ REASON:%@",
|
1999-09-16 07:21:34 +00:00
|
|
|
[super description], _e_name, _e_reason];
|
1999-04-21 20:16:25 +00:00
|
|
|
}
|
1995-03-18 17:15:15 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
1999-04-21 20:16:25 +00:00
|
|
|
void
|
2000-04-05 14:21:05 +00:00
|
|
|
_NSAddHandler (NSHandler* handler)
|
1995-03-18 17:15:15 +00:00
|
|
|
{
|
1999-04-19 14:29:52 +00:00
|
|
|
NSThread *thread;
|
1995-03-18 17:15:15 +00:00
|
|
|
|
1999-04-19 14:29:52 +00:00
|
|
|
thread = GSCurrentThread();
|
|
|
|
handler->next = thread->_exception_handler;
|
|
|
|
thread->_exception_handler = handler;
|
1995-03-18 17:15:15 +00:00
|
|
|
}
|
|
|
|
|
1999-04-21 20:16:25 +00:00
|
|
|
void
|
2000-04-05 14:21:05 +00:00
|
|
|
_NSRemoveHandler (NSHandler* handler)
|
1995-03-18 17:15:15 +00:00
|
|
|
{
|
1999-04-19 14:29:52 +00:00
|
|
|
NSThread *thread;
|
1995-03-18 17:15:15 +00:00
|
|
|
|
1999-04-19 14:29:52 +00:00
|
|
|
thread = GSCurrentThread();
|
|
|
|
thread->_exception_handler = thread->_exception_handler->next;
|
1997-03-03 19:51:04 +00:00
|
|
|
}
|