@ try-catch bloque en Objective-c


¿Por qué @try block no funciona? Se estrelló la aplicación, pero se suponía que debía ser capturado por el bloque @try.

 NSString* test = [NSString stringWithString:@"ss"];

 @try {
    [test characterAtIndex:6];

 }
 @catch (NSException * e) {
    NSLog(@"Exception: %@", e);
 }
 @finally {
    NSLog(@"finally");
 }
Author: Ev., 2010-07-29

3 answers

Todos funcionan perfectamente:)

 NSString *test = @"test";
    unichar a;
    int index = 5;

    @try {
        a = [test characterAtIndex:index];
    }
    @catch (NSException *exception) {
        NSLog(@"%@", exception.reason);
    }
    @finally {
        NSLog(@"Char at index %d cannot be found", index);
        NSLog(@"Max index is: %d", [test length]-1);
    }

Log:

[__NSCFConstantString characterAtIndex:]: Rango o índice fuera de límites

Char en el índice 5 no se puede encontrar

El índice máximo es: 3

 111
Author: iTux,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-29 02:23:59

Ahora he encontrado el problema.

Eliminar el obj_exception_throw de mis puntos de interrupción resolvió esto. Ahora está atrapado por el bloque @try y también, NSSetUncaughtExceptionHandler manejará esto si falta un bloque @try.

 71
Author: Alexandru Circus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-10-31 15:55:15

¿Está seguro de que no es otra cosa porque el código exacto que ha pegado arriba funciona bien?

2010-07-29 16:45:57.677 test[93103:207] Exception: *** -[NSCFString characterAtIndex:]: Range or index out of bounds
2010-07-29 16:45:57.678 test[93103:207] finally
 12
Author: mbogh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-07-29 14:47:46