Error handling like in Rust
Is Mojo going to have error handling like in Rust? With OK an Error Result exposed to the user? Or will it have more traditional way to handle errors, like in Python?
10 Replies
I guess if we have ADT, at least we can have our own Results and Maybes
We could, but support from language syntax will be very handy in such case
I don't know about the future of error handling Mojo but so far trying to be 1:1 with Python exceptions is a goal. Meanwhile this is an interesting article how to combine the best from Go and Rust into a more Pythonic error solution: https://www.inngest.com/blog/python-errors-as-values
Python errors as values: Comparing useful patterns from Go and Rust...
Safer error handling, inspired by Go and Rust
I suspect it will need both. Once mojo gets Algebraic Data Types, you can create your own Sum types like rust's Option and Result. On the other for python compatibility, I would imagine mojo will need some form of Exception handling. If a cpython function is called and it throws an exception, I'm not sure how mojo would handle this. This also begs the question of other FFI boundary related questions (will mojo have a
unsafe
dual world?).If a cpython function is called and it throws an exception, I'm not sure how mojo would handle this.Mojo supports
try
/except
on functions decorated with the raises
keyword (see e.g. https://docs.modular.com/mojo/manual/basics.html#python-integration), and this is used for the Python interop. This is a rather primitive mechanism at present. I am not familiar with plans for the future of error handling in Mojo.Modular Docs - Language basics
Introduction to Mojo's basic language features.
Important to distinguish exceptions from errors, I think.
I wonder if Algebraic Effects Handling/Conditions are on the table?
Maybe. I am not involved with the planning here.
Well, we have traits now so that will be a nice addition for any future standardization of error handling.
Mojo will still require a way to throw exceptions/ panic
Yes, this is how exceptions are currently implemented.