Re: objective-C++ newbie question
Re: objective-C++ newbie question
- Subject: Re: objective-C++ newbie question
- From: "John C. Daub" <email@hidden>
- Date: Mon, 25 Apr 2005 10:37:54 -0500
on 4/25/05 10:09 AM, Keith Ray at email@hidden wrote:
> -(id) init
> {
> self= [super init];
> if ( self != nil )
> {
> myClass1 = new class1;
> }
> }
Since operator new or class1's constructor could throw an exception, you
probably want to wrap up that statement in a try/catch block:
- (id)init {
self = [super init];
if (self != nil) {
try {
myClass1 = new class1;
} catch (...) {
return nil;
}
}
return self; // you forgot this in the original sample. :-)
}
You don't necessarily have to return nil if you catch the C++ exception...
do whatever is appropriate for your class. Maybe it's not fatal if you fail
to allocate the thing, so you just swallow it set myClass1 to NULL, and chug
right along. Heck, maybe in the above setup you know the context of the use
of this code is such that if the C++ exception propagated out is actually a
desirable thing. In the end, it all depends on your code and needs. The
important thing is that the Objective C and C++ exception mechanisms are not
compatible with each other, and you have to be aware of where and how
exceptions from each language could occur and you contend with them
appropriately.
If you haven't already, check these out:
<http://developer.apple.com/releasenotes/Cocoa/Objective-C++.html>
<http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Langua
geOverview/chapter_3_section_10.html>
(and you can find more by plugging "Objective C++" into the search engine on
the http://developer.apple.com/ web site).
--
John C. Daub }:-)>=
<mailto:email@hidden> <http://www.hsoi.com/>
"If it makes you happy, it can't be that bad." - Sheryl Crow
_______________________________________________
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