mochi icon indicating copy to clipboard operation
mochi copied to clipboard

No exception handling?

Open pya opened this issue 10 years ago • 2 comments

There are exceptions:

>>> 1 / 0
Traceback (most recent call last):
  ... site-packages/mochi/core/main.py", line 120, in interact
    eval_tokens(tokens)
  .../site-packages/mochi/core/builtins.py", line 1008, in eval_tokens
    exec(code, global_env)
  File "<string>", line 1, in <module>
ZeroDivisionError: division by zero

But there seems to be no way to catch them:

>>> try:
...     1 / 0
... except:
...     print('got it')
... 
ParsingError: file=<string> lineno=3 colno=7

How would an exception in Mochi be handled?

pya avatar Jul 26 '15 09:07 pya

Currently, Mochi supports only try-except-as statement. It's enough for me so far. https://github.com/i2y/mochi/blob/master/mochi/parser/parser.py#L306

try:
    1/0
except Exception as e:
    print('got it')

i2y avatar Jul 26 '15 10:07 i2y

Ok. Good. Did not know this. Adding the as does th trick. Thanks for pointing this out.

pya avatar Jul 26 '15 10:07 pya