programing

@try-Objective-C에서 catch 블록

new-time 2020. 5. 14. 22:38
반응형

@try-Objective-C에서 catch 블록


@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

블록에 걸리고 블록이 없으면

NSSetUncaughtExceptionHandler

이를 처리합니다

@try

.


Objective-C는 Java가 아닙니다. Objective-C에서는 예외라고합니다. 예외! 오류 처리에 사용하지 마십시오. 그들의 제안이 아닙니다. characterAtIndex를 사용하기 전에 문자열의 길이를 확인하면 모든 것이 잘됩니다 ....참고 URL :

https://stackoverflow.com/questions/3363612/try-catch-block-in-objective-c

반응형