Julia中的NotImplementedException?

时间:2016-03-29 23:30:34

标签: julia

.NET Framework中的C#有一个方便的NotImplementedException,我可以从我打算稍后编写的代码段中抛出它。

朱莉娅有类似的断言吗?

2 个答案:

答案 0 :(得分:4)

只需使用error("unimplemented")throw("unimplemented")即可。这些异常只是为了警告您某些内容未实现,因此您可能不希望通过代码捕获或处理它们。 ErrorException甚至ASCIIString就足够了。

答案 1 :(得分:3)

在Julia中,创建自己的异常类型非常简单。 去年,我将以下Exception类型添加到Julia,以及一种显示我想要的方法:

const UTF_ERR_SHORT             = "invalid UTF-8 sequence starting at index <<1>> (0x<<2>> missing one or more continuation bytes)"
const UTF_ERR_CONT              = "invalid UTF-8 sequence starting at index <<1>> (0x<<2>> is not a continuation byte)"

    type UnicodeError <: Exception
        errmsg::AbstractString      ##< A UTF_ERR_ message
        errpos::Int32               ##< Position of invalid character
        errchr::UInt32              ##< Invalid character
    end

    show(io::IO, exc::UnicodeError) = print(io, replace(replace(string("UnicodeError: ",exc.errmsg),
        "<<1>>",string(exc.errpos)),"<<2>>",hex(exc.errchr)))

现在,要抛出UnicodeError,我可以简单地执行以下操作:

throw(UnicodeError(UTF_ERR_SHORT, pos, chr))

获取一个与我想要的完全相同的异常。

相关问题