• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Are there macros for exception-based error checking?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Are there macros for exception-based error checking?


  • Subject: Re: Are there macros for exception-based error checking?
  • From: Rick Kitts <email@hidden>
  • Date: Tue, 3 May 2005 16:08:27 -0700

Sorry, cocoa newb question.

So in exceptionWithDomain:code:format you do something like:

switch(code){
    case CODEA:
        return [[MyCodeAExceptionClass alloc] initWithStuff:stuff];
    case CODEB:
        return [[MyCodeBExceptionClass alloc] initWithStuff:stuff];
    // etc
}

or is there something more subtle going on?

---Rick

On May 3, 2005, at 2:24 PM, Annard Brouwer wrote:

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:
40objectwarehouse.com


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:
40loudhouse.org


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
References: 
 >Are there macros for exception-based error checking? (From: Larry Gerndt <email@hidden>)
 >Re: Are there macros for exception-based error checking? (From: Annard Brouwer <email@hidden>)

  • Prev by Date: NSWorkspace question
  • Next by Date: Re: Are there macros for exception-based error checking?
  • Previous by thread: Re: Are there macros for exception-based error checking?
  • Next by thread: Re: Are there macros for exception-based error checking?
  • Index(es):
    • Date
    • Thread