如何在Julia中处理异常

时间:2018-01-23 18:05:42

标签: exception exception-handling julia

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

try {

} catch(e) {
     console.log("Exception: " + e);
}

我看过documentation,但我无法理解。

1 个答案:

答案 0 :(得分:6)

The equivalent to your code is:

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

The full structure of try/catch statement is:

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

Parts in square brackets are optional. In particular if you want to omit identifier you should use a newline or ; after catch.