Thursday, August 4, 2022

[FIXED] How to catch C++ exception code in iOS Objective-C?

Issue

I'm new to iOS programming, now I met a problem related to catching the exception code threw from a C++ class.

    @try {
        myCPPClass myObj ; //this throws integer exception codes
    }
    @catch (...) { //I want to catch the integer value here, how ??
        NSLog(@"Exception:") ;
    }
    @finally {
    } 

I knew it maybe not a good practice coding Objective-C in exception catching style, I'd like to know how to make custom exception class for C++ classes in Objective-C ?


Solution

Rename your objective-c .m source files to have the .mm extension. They will then be compiled as objective-c++ which is fully compatible with objective-c while offering all the facilities of c++.

then you can catch c++ exceptions with the usual

try {
   ...
}
catch(std::exception& e) {
   ...
}

construct.



Answered By - Richard Hodges
Answer Checked By - Terry (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.