handle exception in cython functions

@lxyu