[英]@try - catch block in Objective-C
為什么@try阻止工作? 它崩潰了應用程序,但它應該被@try塊捕獲。
NSString* test = [NSString stringWithString:@"ss"];
@try {
[test characterAtIndex:6];
}
@catch (NSException * e) {
NSLog(@"Exception: %@", e);
}
@finally {
NSLog(@"finally");
}
一切都完美:)
NSString *test = @"test";
unichar a;
int index = 5;
@try {
a = [test characterAtIndex:index];
}
@catch (NSException *exception) {
NSLog(@"%@", exception.reason);
NSLog(@"Char at index %d cannot be found", index);
NSLog(@"Max index is: %lu", [test length] - 1);
}
@finally {
NSLog(@"Finally condition");
}
日志:
[__NSCFConstantString characterAtIndex:]:范圍或索引越界
無法找到索引5處的字符
最大指數是:3
最后條件
現在我發現了問題。
從斷點中刪除obj_exception_throw
解決了這個問題。 現在它被@try
塊捕獲了,如果缺少@try
塊, NSSetUncaughtExceptionHandler
將處理它。
Objective-C不是Java。 在Objective-C中,異常就是它們的名稱。 例外! 不要將它們用於錯誤處理。 這不是他們的提議。 在使用characterAtIndex之前檢查一下字符串的長度,一切都很好....
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.