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



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

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



Yes, but not an error most of the time. and the multiple entries following tells you when to use which excepiton type.


Also see