# How to attach an exception to a syntactic location?

**URL:** <https://racket.discourse.group/t/how-to-attach-an-exception-to-a-syntactic-location/3631>\
**Category:** Questions & Answers\
**Tags:** macro, expander, parsers\
**Created:** [March 16, 2025, 4:50pm UTC](https://racket.discourse.group/t/how-to-attach-an-exception-to-a-syntactic-location/3631 "2025-03-16T16:50:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ToddOBryan](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/toddobryan/32/1962_2.png) [@ToddOBryan](https://racket.discourse.group/u/ToddOBryan)\
**Post date:** [March 16, 2025, 4:50pm UTC](https://racket.discourse.group/t/how-to-attach-an-exception-to-a-syntactic-location/3631/1 "2025-03-16T16:50:35Z")

</div>

Following along with Beautiful Racket, I'm trying to implement the AP Computer Science Principles pseudo-code that is used on the exam.

> **[ap-computer-science-principles-exam-reference-sheet.pdf](https://apcentral.collegeboard.org/media/pdf/ap-computer-science-principles-exam-reference-sheet.pdf)**
>
> 470.00 KB

So far, so good. Except that I'd like to have errors attach to the locations in the pseudocode where they're triggered. For example, there is a `NOT x` expression, which only makes sense for booleans. In my expander, I tried both this

```scheme
(define-macro (ps-not EXPR)
  #'(if (not (boolean? EXPR))
          (error (format "NOT must be used with a boolean, given ~a" EXPR))
          (not EXPR)))

```

and this

```scheme
(define (ps-not expr)
  (if (not (boolean? expr))
      (error (format "NOT must be used with a boolean, given ~a" expr))
      (not expr)))

```

but the error points unsurprisingly to the expander, not the file where the error is actually introduced.

The problem (I think) is that I need to have simplified the expression to the point where I can tell it's a boolean and at that point, the original srcloc location is long gone.

I figure there must be a way around this, and it's likely in the Beautiful Racket text, but I don't remember it and can't find it. Any help appreciated.
