在Python中我们可以做到:
try:
import foo
# do stuff
except ImportError:
print("Consider installing package 'foo' for full functionality.")
是否有可能在朱莉娅中发现类似的例外情况?
答案 0 :(得分:4)
目前,Julia没有导入异常机制,您无法将using
置于try-catch块中。
在对这个答案的评论中,问题被提炼为真正要求如何做有条件的包含。以下是如何执行此操作的示例:
# We are making a module that conditionally includes ImageView
module MyModule
# Optional plotting features using ImageView
# The view function is defined in ImageView
export view # Re-export ImageView.view (optional)
try
require("ImageView") # Will throw error if ImageView not installed
global view(args...) = Main.ImageView.view(args...)
catch
# Needs global to put it in the module scope, not the catch scope
global view(args...) = error("ImageView.jl required for drawing!")
# Alternatively, global view(args...) = nothing
end
# ....
end