如何在Julia中处理异常

Yag*_*ias 1 exception-handling exception julia

我想在Julia Lang中处理一个异常,就像使用Javascript一样:

try {

} catch(e) {
     console.log("Exception: " + e);
}
Run Code Online (Sandbox Code Playgroud)

我阅读了文档,但我无法理解.

Bog*_*ski 6

相当于您的代码是:

try
    sqrt(-1) # Code that may throw an exception.
catch y
    warn("Exception: ", y) # What to do on error.
end
Run Code Online (Sandbox Code Playgroud)

try/ catchstatement 的完整结构是:

try
    # Code that may throw an error.
[catch [identifier]
    # What to do if exception is raised.
]
[finally
    # What to do unconditionally when try/catch block exits.
]
end
Run Code Online (Sandbox Code Playgroud)

方括号中的部件是可选的.特别是如果你想省略,identifier你应该使用换行符或;之后catch.