@try - Objective-C 中的 catch 块
为什么@try块不起作用? 它使应用程序崩溃,但它应该被 @try 块捕获。
NSString* test = [NSString stringWithString:@"ss"];
@try {
[test characterAtIndex:6];
}
@catch (NSException * e) {
NSLog(@"Exception: %@", e);
}
@finally {
NSLog(@"finally");
}
Why doesn't @try block work?
It crashed the app, but it was supposed to be caught by the @try block.
NSString* test = [NSString stringWithString:@"ss"];
@try {
[test characterAtIndex:6];
}
@catch (NSException * e) {
NSLog(@"Exception: %@", e);
}
@finally {
NSLog(@"finally");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一切工作都很完美:)
日志:
All work perfectly :)
Log:
现在我发现了问题所在。
从我的断点中删除 obj_exception_throw 解决了这个问题。现在它被
@try
块捕获,并且如果@try
块丢失,NSSetUncaughtExceptionHandler
将处理这个问题。Now I've found the problem.
Removing the
obj_exception_throw
from my breakpoints solved this. Now it's caught by the@try
block and also,NSSetUncaughtExceptionHandler
will handle this if a@try
block is missing.Objective-C 不是 Java。在 Objective-C 中,异常就是这样称呼的。例外!不要将它们用于错误处理。这不是他们的提议。
只需在使用characterAtIndex之前检查字符串的长度,一切都很好......
Objective-C is not Java. In Objective-C exceptions are what they are called. Exceptions! Don’t use them for error handling. It’s not their proposal.
Just check the length of the string before using characterAtIndex and everything is fine....