Home > Software design >  Where do the exception predicates come from in Racket?
Where do the exception predicates come from in Racket?

Time:01-17

I've been trying to figure out how exceptions work in Racket, but I'm a bit puzzled on the exception predicates. To catch an exception of say exn:fail type, you test it with a predicate exn:fail? in a with-handlers expression. I've found the documentation on exn:fail, but I don't see where exn:fail? is documented. Why I'm so curious about this is according to this article, if you make a custom exception struct of say exn:no-vowels, you can still use exn:no-vowels? despite the fact that this predicate was never manually implemented. If I had to wager a guess, the predicate must have been generated automatically with a macro, but if so I'm curious to know where this "automatically generate predicates behavior" is documented and how the macro works exactly.

CodePudding user response:

exn and derived exception types like the exn:fail hierarchy are all instances of Racket structure types, and predicates, constructors and accessors (like exn-message) are automatically defined when they're created with the struct macro.

User defined exception types can be exported from a module with struct-out in a provide form, which exports all those generated functions without you having to list them manually.

  • Related