Exception handling Julia有导入异常机制吗?

Exception handling Julia有导入异常机制吗?,exception-handling,julia,Exception Handling,Julia,在Python中,我们可以执行以下操作: 试试看: 进口食品 #做事 除恐怖外: 打印(“考虑安装包'foo'以获得完整功能。”) 在Julia中是否可能捕获类似的异常?此时Julia没有导入异常机制,并且您不能使用将放入try-catch块中 在对这个答案的评论中,这个问题被细化为真正询问如何进行条件包含。以下是如何做到这一点的示例: # We are making a module that conditionally includes ImageView module MyModule

在Python中,我们可以执行以下操作:

试试看:
进口食品
#做事
除恐怖外:
打印(“考虑安装包'foo'以获得完整功能。”)

在Julia中是否可能捕获类似的异常?

此时Julia没有导入异常机制,并且您不能使用将
放入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

此时,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

听起来他想解决我想向用户展示的一些图片中未解决的问题(以及相关问题),但我不希望这个软件包成为严格的要求。如果未在客户端安装,则应忽略专用于图像显示的代码部分(例如
view()
)。@iaindanning,感谢您的编辑,我将试用它。@iaindanning,请查看更新的问题。“我该如何修复第一次尝试?”Iaindanning,我猜你不明白。
view()
函数是
ImageView
的一部分。如果包不可用,我想使用ImageView:view执行
,然后回退到
view(…)=nothing
。听起来他想要解决中未解决的问题(以及相关问题)。我想向用户展示一些图像,但我不希望此包成为严格要求。如果未在客户端安装,则应忽略专用于图像显示的代码部分(例如
view()
)。@iaindanning,感谢您的编辑,我将试用它。@iaindanning,请查看更新的问题。“我该如何修复第一次尝试?”Iaindanning,我猜你不明白。
view()
函数是
ImageView
的一部分。我想使用ImageView:view执行
,如果包不可用,则返回到
view(…)=nothing