在给定条件的情况下,这两段代码会导致抛出相同的错误:
function f1(x)
if x > 2
return x
else
return error("x <= 2")
end
end
function f2(x)
if x > 2
return x
else
throw(error("x <= 2"))
end
end
Run Code Online (Sandbox Code Playgroud)
当 x <= 2 时同时出现f1
和f2
错误,但我找不到为什么更喜欢一个而不是另一个的任何差异。更进一步,这 3 段代码返回相同的:
error(2)
throw(2)
throw(error(2))
Run Code Online (Sandbox Code Playgroud)
是否有关于如何使用 throw 与直接返回错误的指南?
异步完成多项工作(主要是排序向量和函数计算(其中计算是计算或内存限制的,目前,我可以通过以下方式编写这些操作:
Threads.@spawn
_f1 = Threads.@spawn f1(x)
_f2 = Threads.@spawn f2(x)
_f3 = Threads.@spawn f3(x)
y1 = fetch(_f1)
y2 = fetch(_f2)
y3 = fetch(_f3)
Run Code Online (Sandbox Code Playgroud)
还有这种模式(看起来更干净):
@sync begin
@async y1 = f1(x)
@async y2 = f2(x)
@async y3 = f3(x)
end
Run Code Online (Sandbox Code Playgroud)
对于这个特定的用例,哪一个是首选?