Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 406 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
@try - catch block in Objective-C

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

Reply

#2
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.
Reply

#3
All work perfectly :)

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

Log:

> [__NSCFConstantString characterAtIndex:]: Range or index out of bounds
>
> Char at index 5 cannot be found
>
> Max index is: 3
>
> Finally condition
Reply

#4
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....
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through