How to create custom exception in objective c?

ios, iphone, macos, objective-c

Solution

In the simplest case, I can declare a class using...

@interface CustomException : NSException
@end
@implementation CustomException
@end

...and the code is very much like what you posted:

   @try{
        @throw [[CustomException alloc] initWithName:@"Custom" reason:@"Testing" userInfo:nil];
    }
    @catch(CustomException *ce){
        NSLog(@"Caught custom exception");
    }
    @catch(NSException *e){
        NSLog(@"Caught generic exception");
    }

Problem

I am trying to achieve something like this in objective c. ``` @try{ //some code that will raise exception } @catch(CustomException e){//How to create this //catching mechanism } @catch(NSException e){ //Generic catch } ``` I need to create CustomException class and use it. Could you please help me in creating this CustomException and guide me in how to use this. Thanks in advance.

Original source