How to create custom exception in objective c?

前端 未结 2 1704
有刺的猬
有刺的猬 2021-01-05 18:23

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
//cat         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-05 18:34

    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");
        }
    

提交回复
热议问题