Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NotImplementedException in Julia?

Tags:

julia

C# in the NET Framework has a convenient NotImplementedException, that I can throw from code sections that I intend to write later.

Is there a similar assertion in Julia?

like image 982
becko Avatar asked Mar 29 '16 23:03

becko


2 Answers

Just use error("unimplemented") or throw("unimplemented"). These exceptions are just meant to warn you that something are not implemented, so you might not want to catch or handle them by code. A ErrorException or even ASCIIString is enough.

like image 133
张实唯 Avatar answered Oct 08 '22 01:10

张实唯


In Julia, it's quite simple to create your own exception type. Last year, I added the following Exception type to Julia, along with a method to display exactly how I wanted:

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)))

Now, to throw a UnicodeError, I can simply do something like:

throw(UnicodeError(UTF_ERR_SHORT, pos, chr))

to get an exception that displays exactly as I want it to.

like image 26
Scott Jones Avatar answered Oct 08 '22 03:10

Scott Jones