如何在目标c中创建自定义异常? [英] How to create custom exception in objective c?

查看:75
本文介绍了如何在目标c中创建自定义异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在目标c中实现这样的目标。

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
}

我需要创建CustomException类并使用它。你能帮我创建一下这个CustomException并指导我如何使用它。

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.

提前致谢。

推荐答案

在最简单的情况下,我可以使用...声明一个类

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

这篇关于如何在目标c中创建自定义异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆