代码之家  ›  专栏  ›  技术社区  ›  Kevlar

在Objective-C中返回后,是否最终运行中的代码?

  •  15
  • Kevlar  · 技术社区  · 14 年前

    请考虑以下代码:

    @try {
      if (something.notvalid)
      {
        return;
      }
      // do something else
    } @catch (NSException *ex) {
      // handle exception
    } @finally {
      NSLog(@"finally!");
    }
    

    如果 something 无效,我从try中返回,代码是否在 @finally 执行与否?我认为应该这样做,但我和其他人交谈过,他们不这么认为,我目前无法对此进行测试。

    5 回复  |  直到 6 年前
        1
  •  14
  •   abbood    11 年前

    @最后,代码总是按照 here here .

    @finally块包含的代码 必须执行是否有异常 是否抛出。

        2
  •  4
  •   lewiguez    14 年前

    对。奇怪的是,的确如此。我不知道为什么,但我只是构建了一个测试,并尝试了许多配置,每次都是这样。

    以下是配置文件:

    • Try块中的返回:已停止Try块的执行并导致最终执行
    • 返回到try块并返回finally:停止执行try,停止在finally块和整个方法中执行。
    • 在finally块中返回:与try/catch/finally块外的普通返回类似。
        3
  •  2
  •   user2243927    11 年前

    对于RAI定义,对于特定的资源,finally块将以该代码范围执行。

    它与物体有着密切的关系 ~Destructor . 与对象相同 析构函数 总是执行,最后块也执行。

        4
  •  1
  •   Chand Priyankara    11 年前

    对。即使有一个 Exception 在内部 catch 块, finally 将被执行。

    如果你熟悉C++,那就想想 最后 作为 destructor 一个 object . 无论对象内的语句的状态如何, ~Destructor 将被执行。 但你不能放 return 在内部 最后 [有些编译器允许]。

    请参见下面的代码:查看全局变量 y 被改变了。 也看如何 Exception1 被覆盖 Exception2 .

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace finallyTest
    {
        class Program
        {
            static int y = 0;
            static int testFinally()
            {
                int x = 0;
                try
                {
                    x = 1;
                    throw new Exception("Exception1");
                    x = 2;
                    return x;
                }
                catch (Exception e)
                {
                    x = -1;
                    throw new Exception("Exception2", e);
                }
                finally
                {
                    x = 3;
                    y = 1;
                }
                return x;
            }
    
            static void Main(string[] args)
            {
                try
                {
                    Console.WriteLine(">>>>>" + testFinally());
                }
                catch (Exception e)
                { Console.WriteLine(">>>>>" + e.ToString()); }
                Console.WriteLine(">>>>>" + y);
                Console.ReadLine();
            }
        }
    }
    

    输出:

        >>>>>System.Exception: Exception2 ---> System.Exception: Exception1
       at finallyTest.Program.testFinally() in \Projects\finallyTest\finallyTest\Program.cs:line 17
       --- End of inner exception stack trace ---
       at finallyTest.Program.testFinally() in \Projects\finallyTest\finallyTest\Program.cs:line 24
       at finallyTest.Program.Main(String[] args) in \Projects\finallyTest\finallyTest\Program.cs:line 38
    >>>>>1
    
        5
  •  0
  •   Cris    6 年前

    是的,这是一个示例片段,输出是

    尝试! 抓住! 终于!

    @try {
        NSLog(@"try!");
    
        NSException *e = [NSException
                          exceptionWithName:@"No Name"
                          reason:@"No Reason"
                          userInfo:nil];
        @throw e;
    
    
    } @ catch (...)
    {
        NSLog(@"catch!");
        return;
    }
    @finally
    {
        NSLog(@"finally!");
    }
    
    NSLog (@"other code");