Re: Are there macros for exception-based error checking?
Re: Are there macros for exception-based error checking?
- Subject: Re: Are there macros for exception-based error checking?
- From: Annard Brouwer <email@hidden>
- Date: Tue, 3 May 2005 23:24:45 +0200
Hi,
This is what I have done:
@interface NSException (ABCategories)
+ (id)exceptionWithFormat:(NSString *)aFormat, ...;
+ (id)exceptionWithDomain:(NSString *)aDomain
code:(int)aCode
format:(NSString *)aFormat, ...;
@end
Then in your code:
@throw [NSException exceptionWithDomain: XXX code: YYY format: @"ZZZ"];
And now you can return an instance of the correct exception based on
the error code. Very easy to read, very OO and clean. I'm using the
Java convention to create specific subclasses of NSException that
encapsulate a specific error situation (one of the nice aspects of that
language).
Now what I'd really like to do is to attach a stack trace to the
exception as well. Then I don't need a debugger anymore. If anybody as
a pointer on how to do this I'd be mighty happy!
Cheers!
Annard
On 3 May 2005, at 02:25, Larry Gerndt wrote:
In CodeWarrior/PowerPlant, I was used to writing code like this:
try {
ThrowIfOSStatus_( SomeFunctionThatReturnsOSStatus() );
ThrowIfOSStatus_( SomeFunctionThatReturnsOSStatus() );
ThrowIfOSStatus_( SomeFunctionThatReturnsOSStatus() );
} catch(LException &inErr) {
handling code
}
ThrowIfOSStatus_ is a macro which will throw an exception if an error
status is returned. I've grown to love this kind of thing because it
makes code easier to read than something like this:
OSStatus err;
err = SomeCarbonFunctionThatReturnsOSStatus()
if (err==noErr)
err = SomeCarbonFunctionThatReturnsOSStatus()
if (err==noErr)
err = SomeCarbonFunctionThatReturnsOSStatus()
I was surprised that I could not find such macros in NSFoundation or
somewhere in Cocoa, nor have I yet seen something like in anyone
else's Cocoa code. Is there some good reason why this is so, or have
I missed something?
P.S. My solution for now was to use the NSCAssert in a macro:
#define ThrowIfOSStatus_(condition) NSCAssert(noErr==condition, @"")
Is this a good approach?
--
Larry Gerndt
AIM Handle: SonOfTheSonOfMan
Let the truth be told though the heavens fall -- James Garrison
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
email@hidden
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden