juliafield

What exception does getfield throw?


What exception does the following piece of code throw?

getfield(nothing, :a)

ERROR: type Nothing has no field a seems to be only catchable via Exception by trial and error, every documentation left me clueless...

match_strings = try @. getfield(match(regex, words), :match)
catch e
    if isa(e, Exception) return false end
    throw(e)
end

(snippet from a function)
Follow up: Is nothing.a / getproperty easier to catch if getfield is not?


Solution

  • It throws a simple ErrorException. You can check by catching the error and printing the type:

    julia> try
               getfield(nothing, :a)
           catch e
               println(typeof(e))
           end
    # ErrorException
    

    getproperty(nothing, :a) throws the same, as does attempting to access property a via nothing.a.