如何在目标c中捕获exc_arithmetic

时间:2012-05-22 19:08:01

标签: objective-c

我试图在我的代码中捕获除以0算术异常,但看起来NSException没有捕获它并且程序以消息exc_arthimetic错误终止。什么是捕获异常的方法

     @try {

         int k;

         printf("please enter k value \n");
         scanf("%i",&k);

         int j=1;

         // testing division 1/0 condition
         int i=j/k;
         NSLog(@" value of i= %i",i);
     }

     @catch (NSException *exception) {
         NSLog(@"exception is = %@  reason is %@",[exception name],[exception reason]);
     }
     @finally {
         NSLog(@"inside finally");
     }


     NSLog(@" last line");

1 个答案:

答案 0 :(得分:2)

exc_arthimetic不是例外,而是OS发送的信号。我建议在尝试划分之前检查分母是否为零。

为了捕获这个信号更复杂,可能不值得努力。

看一看 Learning Objective-C. Using Xcode 3.2.1. What is error: Program received signal: “EXC_ARITHMETIC”