# Import
int main (int argc, const char * argv [])
{
@ Autoreleasepool {
@ Try
{
int x = 1, y = 0;
double c = (double) x / y;
NSLog (@ "c =% f", c);
}
@ Catch (NSException * e)
{
NSLog (@ "\ nCaught% @% @", [e name], [e reason]);
}
@ Finally
{
NSLog (@ "Finally OK!!");
}
}
return 0;
}
Running Results:
2012-07-19 19:22:13.537 002 [2193:4503] c = inf
2012-07-19 19:22:13.540 002 [2193:4503] Finally OK!!
Divisor is zero, and why the program does not jump to catch, and catch the exception it? But a normal operation. . .
Reply:
Is double type.
Reply:
This looks and double type does not matter, mainly OC automatic processing of the divisor is zero.
From the results point of view, "c = inf", which, inf represents an infinite number, so it will not throw an exception
I found a solution, a custom exception class object, if equal to 0, then it is thrown.
After the call when the main function will put it try inside, if there is an exception object, catch can be thrown up
Reply:
Try not to judge the correctness of normal data processing in unusual ways. . . . . .
Reply:
Ah, but just wanted a way to try catch throw practice exercises, hee hee
No comments:
Post a Comment