Best way to enforce argument requirements (assert vs. exception, or something else)?

question

#1

I have a function that expects a positive number as argument, for example:

f(x) = sqrt(x)

I want to be safe about this, so I want to throw an error if x < 0. Currently I do an assert:

function f(x)
  @assert x >= 0
  sqrt(x)
end

Is this the recommended way? Or should I throw an error instead? Are there guidelines for this?


#2

No.

Yes, but not an error most of the time.

https://docs.julialang.org/en/latest/manual/control-flow/#Built-in-Exceptions-1 and the multiple entries following https://docs.julialang.org/en/latest/stdlib/base/#Core.ArgumentError tells you when to use which excepiton type.


#3

Also see