Up
Authors
- Adam Fedor (
fedor@boulder.colorado.edu
)
-
Date: 2006-02-11 11:22:57 -0700 (Sat, 11 Feb 2006)
Copyright: (C) 1995, 1996 Free Software Foundation, Inc.
- Declared in:
- Foundation/NSException.h
Availability: OpenStep
NSAssertionHandler objects are used to raise exceptions on behalf of macros implementing assertions.
Each thread has its own assertion handler instance.
The macros work together with the assertion handler object to produce meaningful exception messages containing the name of the source file, the position within that file, and the name of the ObjC method or C function in which the assertion failed.
An NSAssertionHandler instance is created on demand for each thread and is stored in the thread's dictionary under the key NSAssertionHandler. A custom NSAssertionHandler can be used by adding it to the thread dictionary under this key.
The assertion macros are: NSAssert()
, NSCAssert()
, NSAssert1()
, NSCAssert1()
, NSAssert2()
, NSCAssert2()
, NSAssert3()
, NSCAssert3()
, NSAssert4()
, NSCAssert4()
, NSAssert5()
, NSCAssert5()
, NSParameterAssert()
, NSCParameterAssert()
Method summary
+ (
NSAssertionHandler*)
currentHandler;
Availability: OpenStep
Returns the assertion handler object for the current thread.
If none exists, creates one and returns it.
- (void)
handleFailureInFunction: (
NSString*)functionName
file: (
NSString*)fileName
lineNumber: (int)line
description: (
NSString*)format
,...;
Availability: OpenStep
Handles an assertion failure by using
NSLogv()
to print an error message built from the supplied arguments, and then raising an NSInternalInconsistencyException
- (void)
handleFailureInMethod: (SEL)aSelector
object: (id)object
file: (
NSString*)fileName
lineNumber: (int)line
description: (
NSString*)format
,...;
Availability: OpenStep
Handles an assertion failure by using
NSLogv()
to print an error message built from the supplied arguments, and then raising an NSInternalInconsistencyException
- Declared in:
- Foundation/NSException.h
- Conforms to:
- NSCoding
- NSCopying
Availability: OpenStep
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.
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:
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
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 exception is using the +raise:format:,...
method.
If there is no NS_HANDLER... NS_ENDHANDLER block enclosing (directly or indirectly) code where an exception is raised, then control passes to the uncaught exception handler function and the program is then terminated.
The uncaught exception handler is set using NSSetUncaughtExceptionHandler()
and if not set, defaults to a function which will simply print an error message before the program terminates.
Instance Variables
Method summary
+ (
NSException*)
exceptionWithName: (
NSString*)name
reason: (
NSString*)reason
userInfo: (
NSDictionary*)userInfo;
Availability: OpenStep
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.
+ (void)
raise: (
NSString*)name
format: (
NSString*)format
,...;
Availability: OpenStep
Creates an exception with a
name and a reason using the
format string and any additional arguments. The exception is then
raised using the
-raise
method.
+ (void)
raise: (
NSString*)name
format: (
NSString*)format
arguments: (va_list)argList;
Availability: OpenStep
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 using the
-raise
method.
- (id)
initWithName: (
NSString*)name
reason: (
NSString*)reason
userInfo: (
NSDictionary*)userInfo;
Availability: OpenStep
This is a designated initialiser for the class.
Initializes a newly allocated NSException object with a name, reason and a dictionary userInfo.
- (
NSString*)
name;
Availability: OpenStep
Returns the name of the exception.
- (void)
raise;
Availability: OpenStep
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.
If the exception was not caught in a macro, the currently set uncaught exception handler is called to perform final logging and the program is then terminated.
If the uncaught exception handler fails to terminate the program, then the default behavior is to terminate the program as soon as the uncaught exception handler function returns.
NB. all other exception raising methods call this one, so if you want to set a breakpoint when debugging, set it in this method.
- (
NSString*)
reason;
Availability: OpenStep
Returns the exception reason.
- (
NSDictionary*)
userInfo;
Availability: OpenStep
Returns the exception userInfo dictionary.
Instance Variables for NSException Class
@protected NSDictionary* _e_info;
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.
@protected NSString* _e_name;
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.
@protected NSString* _e_reason;
Warning the underscore at the start of the name of this instance variable indicates that, even though it is not technically private, it is intended for internal use within the package, and you should not use the variable in other code.
Up