On 15 Jan 2009, at 3:53 pm, Kyle Sluder wrote:

This is known as a "continuation".  Scheme, for example, often uses
them instead of exceptions.  Much like a jmp_buf, It contains all of
the information necessary to resume execution at the point the
continuation is created.  In the event of an error, an error handler
is invoked with the continuation object, at which point the error
handler can inspect the error condition and potentially modify the
state such that execution can continue to proceed.  It then executes
the continuation and the program resumes where it left off.


OK, interesting...



This isn't the model that exceptions follow, unfortunately.  By the
time your @catch block has caught the exception, the stack has already
been unwound and you can't get it back.


That's OK for the stack in general. The problem seems to be it leaves the graphics context stack out of whack. I can obviously deal with this by @catching wherever I have used saveGraphicsState and calling restoreGraphicsState in my catch block before rethrowing, but it's really clunky.

What I tried was this:

// at my very top level, i.e. [NSView drawRect:]

NSGraphicsContext* topContext = [[NSGraphicsContext currentContext] retain];

@try
{
// make all my other drawing calls, any of which might save/restore the graphics context but also could throw an exception

}
@catch( NSException* exc )
{
        // we're back at the top, just ignore the exception here
}

[NSGraphicsContext setCurrentContext:topContext];
[topContext release];


Which actually seems to work - the view is no longer screwed up and unusable following an exception. However, I'm not sure this is safe, as it's blowing away all the stacked graphics contexts. While that's what I want, I don't know if I'm allowed to just forget they ever existed in this way.

--Graham


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to