diff --git a/Solver/functionPython.h b/Solver/functionPython.h
index 303d9dad88790be2aa51d317df4dcf555d5d8216..0a87f753796ead0c4edb872842af865df358eeb2 100644
--- a/Solver/functionPython.h
+++ b/Solver/functionPython.h
@@ -20,7 +20,14 @@ class functionPython : public function {
   {
     R.setAsProxy(res); 
     R.setDataCacheMap(m);
-    PyEval_CallObject(_pycallback, _pyargs);
+    PyObject *result = PyEval_CallObject(_pycallback, _pyargs);
+    if (result) {
+      Py_DECREF(result);
+    }
+    else {
+      PyErr_Print();
+      Msg::Fatal("An error occurs in the python function.");
+    }
   }
   functionPython (int nbCol, PyObject *callback, std::vector<const function*> dependencies)
     : function(nbCol), _pycallback(callback)
diff --git a/gmshpy/gmshCommon.i b/gmshpy/gmshCommon.i
index d76c61e5a8999a9fe15f150211cd8bb1e60dfc91..6d4dc71764d429dc1656c322819a707dac8f33f7 100644
--- a/gmshpy/gmshCommon.i
+++ b/gmshpy/gmshCommon.i
@@ -24,7 +24,7 @@
 #endif
       std::cout<<level<<" : "<<message<<std::endl;
       if (level == "Fatal") 
-        throw;
+        Msg::Exit(1);
     }
   };
 %}