为什么最终阻止错误?

4th*_*ace 3 julia

finally这里的部分:http://docs.julialang.org/en/release-0.4/manual/control-flow/#finally-clauses,他们使用这个例子:

f = open("file")
try
    # operate on file f
finally
    close(f)
end
Run Code Online (Sandbox Code Playgroud)

当我在REPL中运行类似的代码时,会发生这种情况:

julia> f = open("myfile.txt")
IOStream(<file myfile.txt>)

julia> try
       sqrt(-10)
       finally 
         close(f)
       end
ERROR: DomainError:
 [inlined code] from none:2
 in anonymous at no file:0
Run Code Online (Sandbox Code Playgroud)

知道有什么区别吗?

Fen*_*ang 5

finallycatch例外.这是为了保证无论是否发生异常,都会发生清理步骤.注意区别:

try
    sqrt(-10)
catch
    println("Exception swallowed!")
end
Run Code Online (Sandbox Code Playgroud)

try
    sqrt(-10)
finally
    println("This cleanup happened regardless of whether an exception was thrown.")
end
Run Code Online (Sandbox Code Playgroud)

通常一个人结合catchfinally:

try
    sqrt(-10)
catch
    println("Swallowed exception.")
finally
    println("...but finally ran regardless.")
end
Run Code Online (Sandbox Code Playgroud)