1

Suppose I'm using a package function f that takes a single argument x and performs some error checking on that argument. Like, is x of the right type? Etc. And if there is an error then f(x) throws an error with Error().

Is there an easy way to write a wrapper function fCatch around f to catch that error, and say return false if f(x) throws an error but true otherwise? The naïve way to accomplish this same effect would be to copy the code for f and change all the Error(...); lines into a return false; and set a return true; at the end, but it's bad form to duplicate all the error-checking code.

Mike Pierce
  • 1,390
  • 1
  • 12
  • 35
  • 1
    There is an undocumented `CALL_WITH_CATCH` - see e.g how I've used it in this pull request https://github.com/gap-packages/primgrp/pull/34 (note `BreakOnError` setting changed there to make it work the way I need). It is also used in the SCSCP package: https://github.com/gap-packages/scscp/blob/master/lib/server.gi – Olexandr Konovalov Jul 04 '20 at 17:51
  • @AlexanderKonovalov Thanks! Works nearly perfectly. The error doesn't cause the program to break, but the Error message still get's printed. Is there a way to suppress this? – Mike Pierce Jul 05 '20 at 07:20
  • You can try to start GAP with -q option (quiet mode). But maybe that will be too much quietness... – Olexandr Konovalov Jul 05 '20 at 17:17

1 Answers1

1

As per Alexander Konovalov's comment, the following works, except the Error(...) message that f(x) triggers still prints.

fCatch := function(x)
local result;
    BreakOnError := false;
    result := CALL_WITH_CATCH(f, [ x ])[1];;
    BreakOnError := true;
    return result;
end;

A warning: the CALL_WITH_CATCH function, and any other function with an ALL CAPS name, is undocumented and intended to only be used internally in GAP.

Mike Pierce
  • 1,390
  • 1
  • 12
  • 35
  • You can pass `f` as the 1st argument, and allow x to be a list of arguments, then call this as `fCatch(funcname,listargs);` Also, `result` is a list of length 2, the 1st entry is `true` or `false`, the 2nd the result of the successful call or the error message. Maybe you want to analyse the 1st and return the 2nd. Note also that this is undocumented and hence should be used with care. I would be interested to hear some more details about your particular use case, to see if it could be done differently. – Olexandr Konovalov Jul 07 '20 at 09:09
  • @AlexanderKonovalov Yeah I'd modify this for my case, but I wanted to ask a more general question here. I'm using [QPA](https://www.gap-system.org/Manuals/pkg/QPA-1.25/doc/chap0.html) and I'm writing a function that feeds a whole bunch of different matrices to [RightModuleHomOverAlgebra](https://www.gap-system.org/Manuals/pkg/QPA-1.25/doc/chap7.html#X8318ED607FE21F55). Most of these matrices wouldn't be homomorphisms, so I wanted the function to ignore the errors and return only the valid homomorphisms rather than break. – Mike Pierce Jul 08 '20 at 02:57
  • 1
    Thanks - in this case, I believe that a cleaner and robust approach would actually be to check properly if a matrix defines a homomorphism. We usually discourage users from using functions names with ALL_CAPS as those are internal. – Olexandr Konovalov Jul 08 '20 at 09:07
  • 1
    I really like GAP syntax highlighting here - thanks for starting to edit some older questions to add it. Seems a new feature after they [switched to CommonMark](https://meta.stackexchange.com/questions/348746/were-switching-to-commonmark) - but just tried it at [Mathematics site](https://math.stackexchange.com/) and it's not supported there :( – Olexandr Konovalov Jul 08 '20 at 09:24
  • @AlexanderKonovalov That's what I did at first, is just copy the source for RightModuleHomOverAlgebra, take out the error checking part of the function (which was most of it) and change all the `Error()` bits to `return false;` bits. It did feel silly to duplicate those 50 or so lines of code though. – Mike Pierce Jul 08 '20 at 16:18
  • @AlexanderKonovalov I'm glad you like it! :) Yeah [syntax highlighting isn't supported on MathSE](https://meta.stackexchange.com/questions/314553/which-stack-exchange-sites-have-syntax-highlighting-enabled). It looks like the syntax highlighting could be made automatic based on the [tag:gap-system] tag being used though. I've [asked on MetaSO if this can be enabled](https://meta.stackoverflow.com/q/399188/2671341). – Mike Pierce Jul 08 '20 at 16:35
  • @AlexanderKonovalov So it looks like there isn't actually any GAP highlighting. StackOverflow is just using the `default` code highlighter. [A mod over on the MetaSO post](https://meta.stackoverflow.com/questions/399188/could-we-have-automatic-syntax-highlighting-on-the-gap-system-tag#comment781440_399189) said they could change it to where GAP code uses `default` highlighting by default. Since you're [officially the top (gap-system) user](https://stackoverflow.com/tags/gap-system/topusers), would you like to weigh in on this? – Mike Pierce Jul 12 '20 at 20:52
  • Thanks - done. Also, would you mind adding some "disclaimers" about undocumented ALL_CAPS functions to your post, to make them more visible than in comments? – Olexandr Konovalov Jul 12 '20 at 22:25
  • @AlexanderKonovalov Yeah sure. And generally, please feel free to edit any of my questions or answers if you think something needs to be said. :) – Mike Pierce Jul 13 '20 at 14:40
  • Thanks! You may also be interested to look at https://meta.stackoverflow.com/questions/399410/ – Olexandr Konovalov Jul 15 '20 at 18:08