Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to catch HyMacroExpansionError? #802

Closed
weakish opened this issue May 7, 2015 · 4 comments
Closed

How to catch HyMacroExpansionError? #802

weakish opened this issue May 7, 2015 · 4 comments

Comments

@weakish
Copy link
Contributor

weakish commented May 7, 2015

Tried:

  • (import [hy.errors [HyMacroExpansionError]])
  • try to catch HyTypeError, TypeError and Exception

All failed.

Example:

(import [hy.errors [HyMacroExpansionError]])

(defmacro foo [a] a)
(try (foo 1 2) (catch [e HyMacroExpansionError] (print "catched"))) ;; 1
(try (foo 1 2) (catch [e HyTypeError] (print "catched"))) ;; 2
(try (foo 1 2) (catch [e TypeError] (print "catched"))) ;; 3
(try (foo 1 2) (catch [e Exception] (print "catched"))) ;; 4
(try (foo 1 2) (catch [] (print "catched"))) ;;  5

1-5 all fails with throwing HyMacroExpansionError:

  (try (foo 1 2) (catch [e Exception] (print "catched"))) ;; 4
       ^-------^
HyMacroExpansionError: `foo' takes exactly 1 argument (2 given)

Tested on hy 0.10.1 (Python 2.7.6) and hy commit 42983d1 (Python Python 3.4.0)

@algernon
Copy link
Member

algernon commented May 7, 2015

I'm not sure you can catch this. Macro expansion happens at compile time, while try/catch at runtime. By the time Hy gets to the (try (catch ...)) block, the macro expansion already happened, failed, and raised an exception.

@refi64
Copy link
Contributor

refi64 commented May 7, 2015

The reason is that macros get expanded before the program is run. Therefore, the exception occurs before the program even begins.

@weakish
Copy link
Contributor Author

weakish commented May 12, 2015

Thanks. Looking forward better debugging support for macros. #741

@weakish weakish closed this as completed May 12, 2015
@emidln
Copy link

emidln commented May 12, 2015

You could build your own defmacro that works inside a try/catch if you were so inclined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants