Taichi: [Python] Standardize exception handling

Created on 27 May 2020  路  2Comments  路  Source: taichi-dev/taichi

Two nice tips raised by @rexwangcc:

  • It seems we've been using assert over raise Exception acorss the codebase, while they works similar in most cases, assert statements are removed when the Py compilation is optimized (e.g. python -O examples/*.py), and thus assertions will be unsafely bypassed. Do we have special considerations about why we chose asserts?
  • For the places that are raising exceptions, I noticed often wild Exception is being thrown, which might not be a good practice to me. As we start to discuss refactor the operations and math libs, it might be a good chance to have more explicit Exceptions defined in exception.py?

There seems to be a few things to decide:

Discussions are welcome!

My personal opinions: Yes, Yes, No (we should first try to reuse standard Python exceptions).

discussion welcome contribution

Most helpful comment

Agreed. I may be of help in this, as I'm currently refactoring the matrix and math api, where I've found some of the error messages (resulted from incorrect usage) to be really cryptic.....

All 2 comments

I think the distinction between assertions and exceptions are: whether this is a violation of the system invariants, or an error possibly due to something we cannot control (e.g. user input error, OOM, etc). In another word, assertions are something that should never fail. Otherwise we should throw an exception.

...thus assertions will be unsafely bypassed

Good point. assert() in C/C++ could also be compiled away in release build. So I think an implication is that we should never put any expression with side-effects inside any sort of assertions in any language.

Agreed. I may be of help in this, as I'm currently refactoring the matrix and math api, where I've found some of the error messages (resulted from incorrect usage) to be really cryptic.....

Was this page helpful?
0 / 5 - 0 ratings

Related issues

archibate picture archibate  路  4Comments

quadpixels picture quadpixels  路  3Comments

yuanming-hu picture yuanming-hu  路  3Comments

archibate picture archibate  路  4Comments

GeoffreyPlitt picture GeoffreyPlitt  路  4Comments