[Gretl-devel] "catch" in a user-defined function

Artur T. artur.tarassow at googlemail.com
Fri Apr 8 07:12:19 EDT 2016


That was very helpful, Allin! Thank you for this.
This problem is solved now.

Best,
Artur


Am 07.04.2016 um 20:04 schrieb Allin Cottrell:
> 
> <output>
> ? catch mI = matI(mat)
> In regard to function matI:
> Warning: "catch" should not be used on calls to user-defined functions
> </output>
> 
> This should be taken seriously. For "catch" to work reliably it must be
> applied directly to a built-in command or function. On return from a
> user-defined function it's too late to ensure integrity of gretl's state
> in case of error, so (although you may be OK in simple cases) problems
> such as you mention are to be expected.
> 
> You should move the "catch" into the function where inv() is called.
> Here's one possible approach:
> 
> <hansl>
> function matrix try_invert (matrix m, scalar *err)
>     catch matrix ret = inv(m)
>     err = $error
>     if err
>         ret = {}
>     endif
>     return ret
> end function
> 
> mat = zeros(2,2)
> scalar err = 0
> mI = try_invert(mat, &err)
> if !err
>     printf "WORKS\n"
> else
>     printf "FAILS\n"
> endif
> </hansl>
> 
> And here's another:
> 
> <hansl>
> function scalar try_invert (matrix m, matrix *minv)
>     catch minv = inv(m)
>     return $error
> end function
> 
> mat = zeros(2,2)
> matrix minv
> err = try_invert(mat, &minv)
> if !err
>     printf "WORKS\n"
> else
>     printf "FAILS\n"
> endif
> </hansl>
> 
> Allin
> 
> _______________________________________________
> Gretl-devel mailing list
> Gretl-devel at lists.wfu.edu
> http://lists.wfu.edu/mailman/listinfo/gretl-devel


More information about the Gretl-devel mailing list