Maturity Index: Experimental
The Portable Object Compiler uses blocks for error handling i.e., the processing of exceptional conditions that interrupt the normal flow of program execution, such as out of memory, division by zero etc.
The Object method error: and the Block instance method ifError: are used for this.
Blocks are a non-standard part of the Objective-C language; this implementation, the first and most powerful as far as we know, is based on Brad Cox's TaskMaster paper where the same concept is discussed under the name of Action Expressions.
For a discussion on what Blocks are, and how to use them, we refer to the document Objective-C Blocks.
The compiler has an option -noBlocks that can be used to turn off the special syntax for Blocks.
+errorHandlerReturns the default handler, as set by errorHandler:, or, if none was explicitely set, it uses the following handler:
id errorHandler = { :msg :rcv | fprintf(stderr,[msg str]);abort(); };
+errorHandler:aHandlerMake aHandler the default error handler. Returns the handler that was previously registered as default handler.
-ifError:aHandlerEvaluates the receiver of the message and returns its return value. If an exception is raised, evaluates aHandler. This works by pushing aHandler on a stack of error handlers, the method halt: temporarily pops off an error handler and evaluates it with a message and receiver object as arguments :
Note: It is an error to have a non-local return from within the receiver, as this would leave an error handler dangling. It is allowed to return however, from within the error handler.[ { ... } ifError: { :msg :rcv | ... }];
-value:anObjectifError:aHandlerLike ifError: but the receiving block can take an argument.
-valueEvaluates the receiver of the message and returns its return value.
- (int)intvalueEvaluates the receiver of the message and returns its return value.
-atExitEvaluates the receiver of the message when the process exits. See the ANSI C function atexit() for more details. There's a maximum of 32 exit Blocks that can be registered to be automatically called on exit. Blocks are evaluated in reverse order of their registration.
-value:anObjectEvaluates, with anObject as argument, the receiver of the message and returns its return value.
- (int)intvalue:anObjectEvaluates, with anObject as argument, the receiver of the message and returns its return value.
-value:firstObjectvalue:secondObjectEvaluates the receiver of the message with two arguments and returns its return value.
- (int)intvalue:firstObjectvalue:secondObjectEvaluates the receiver of the message with two arguments and returns its return value.
-repeatTimes:(int)nMethod to evaluate the receiver Block n times. Similar to the Smalltalk method timesRepeat: but with argument and receiver interchanged. Returns self.