Improve catching exceptions

This commit is contained in:
Kovid Goyal 2016-05-06 00:45:27 +05:30
parent ea82680617
commit 2ce3aad03e

View File

@ -7,6 +7,10 @@
%Import QtGui/QtGuimod.sip %Import QtGui/QtGuimod.sip
%ModuleCode %ModuleCode
#include <imageops.h> #include <imageops.h>
#define CATCH \
} catch (std::bad_alloc &) { PyErr_NoMemory(); return NULL; \
} catch (std::exception &exc) { PyErr_SetString(PyExc_RuntimeError, exc.what()); return NULL; \
} catch (...) { PyErr_SetString(PyExc_RuntimeError, "unknown error"); return NULL;}
%End %End
QImage* remove_borders(const QImage &image, double fuzz); QImage* remove_borders(const QImage &image, double fuzz);
@ -14,7 +18,7 @@ QImage* remove_borders(const QImage &image, double fuzz);
try { try {
sipRes = remove_borders(*a0, a1); sipRes = remove_borders(*a0, a1);
if (sipRes == NULL) return NULL; if (sipRes == NULL) return NULL;
} catch (std::bad_alloc) { PyErr_NoMemory(); return NULL; } CATCH
%End %End
QImage* grayscale(const QImage &image); QImage* grayscale(const QImage &image);
@ -22,5 +26,5 @@ QImage* grayscale(const QImage &image);
try { try {
sipRes = grayscale(*a0); sipRes = grayscale(*a0);
if (sipRes == NULL) return NULL; if (sipRes == NULL) return NULL;
} catch (std::bad_alloc) { PyErr_NoMemory(); return NULL; } CATCH
%End %End