diff --git a/modules/java/generator/src/cpp/Mat.cpp b/modules/java/generator/src/cpp/Mat.cpp
index f2ad94e38..5aa939ec8 100644
--- a/modules/java/generator/src/cpp/Mat.cpp
+++ b/modules/java/generator/src/cpp/Mat.cpp
@@ -1,12 +1,71 @@
 #define LOG_TAG "org.opencv.core.Mat"
 
+#include <stdexcept>
+
 #include "common.h"
 #include "opencv2/core/core.hpp"
 
 using namespace cv;
 
-extern "C" {
+/// throw java exception from std::exception, no logging
+static const char *throwJavaException(JNIEnv *env, const std::exception &e);
+static const char *throwJavaException(JNIEnv *env, const std::exception &e) {
+  const char *exception_str = "std::exception";
+  jclass je = 0;
+  
+  if(dynamic_cast<const cv::Exception*>(&e)) {
+    exception_str = "cv::Exception";
+    je = env->FindClass("org/opencv/core/CvException");
+  }
+  
+  if(!je) je = env->FindClass("java/lang/Exception");
+  env->ThrowNew(je, e.what());
+  return exception_str;
+}
 
+/// throw java exception from unknown exception, no logging
+static void throwJavaException(JNIEnv *env);
+static void throwJavaException(JNIEnv *env) {
+  jclass je = env->FindClass("java/lang/Exception");
+  env->ThrowNew(je, "unknown exception");
+}
+
+/// throw java exception from std::exception, use LOGD
+static void throwJavaExceptionD(JNIEnv *env, const std::exception &e, const char *method);
+static void throwJavaExceptionD(JNIEnv *env, const std::exception &e, const char *method) {
+  const char *exception_str = throwJavaException(env, e);
+  LOGD("%s caught %s: %s", method, exception_str, e.what());
+  (void)method;        // avoid "unused" warning
+  (void)exception_str; // avoid "unused" warning
+}
+
+/// throw java exception from unknown exception, use LOGD
+static void throwJavaExceptionD(JNIEnv *env, const char *method);
+static void throwJavaExceptionD(JNIEnv *env, const char *method) {
+  throwJavaException(env);
+  LOGD("%s caught unknown exception (...)", method);
+  (void)method; // avoid "unused" warning
+}
+
+/// throw java exception from std::exception, use LOGE
+static void throwJavaExceptionE(JNIEnv *env, const std::exception &e, const char *method);
+static void throwJavaExceptionE(JNIEnv *env, const std::exception &e, const char *method) {
+  const char *exception_str = throwJavaException(env, e);
+  LOGE("%s caught %s: %s", method, exception_str, e.what());
+  (void)method;        // avoid "unused" warning
+  (void)exception_str; // avoid "unused" warning
+}
+
+/// throw java exception from unknown exception, use LOGE
+static void throwJavaExceptionE(JNIEnv *env, const char *method);
+static void throwJavaExceptionE(JNIEnv *env, const char *method) {
+  throwJavaException(env);
+  LOGE("%s caught unknown exception (...)", method);
+  (void)method; // avoid "unused" warning
+}
+
+extern "C" {
+  
 
 //
 //   MatXXX::MatXXX()
@@ -35,22 +94,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__III
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__III
   (JNIEnv* env, jclass, jint rows, jint cols, jint type)
 {
+    static const char method_name[] = "Mat::n_1Mat__III()";
     try {
-        LOGD("Mat::n_1Mat__III()");
+        LOGD(method_name);
 
         Mat* _retval_ = new Mat( rows, cols, type );
 
         return (jlong) _retval_;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1Mat__III() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1Mat__III() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1Mat__III()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -67,22 +122,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__DDI
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__DDI
   (JNIEnv* env, jclass, jdouble size_width, jdouble size_height, jint type)
 {
+    static const char method_name[] = "Mat::n_1Mat__DDI()";
     try {
-        LOGD("Mat::n_1Mat__DDI()");
+        LOGD(method_name);
         Size size((int)size_width, (int)size_height);
         Mat* _retval_ = new Mat( size, type );
 
         return (jlong) _retval_;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1Mat__DDI() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1Mat__DDI() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1Mat__DDI()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -100,22 +151,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__IIIDDDD
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__IIIDDDD
   (JNIEnv* env, jclass, jint rows, jint cols, jint type, jdouble s_val0, jdouble s_val1, jdouble s_val2, jdouble s_val3)
 {
+    static const char method_name[] = "Mat::n_1Mat__IIIDDDD()";
     try {
-        LOGD("Mat::n_1Mat__IIIDDDD()");
+        LOGD(method_name);
         Scalar s(s_val0, s_val1, s_val2, s_val3);
         Mat* _retval_ = new Mat( rows, cols, type, s );
 
         return (jlong) _retval_;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1Mat__IIIDDDD() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1Mat__IIIDDDD() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1Mat__IIIDDDD()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -132,23 +179,19 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__DDIDDDD
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__DDIDDDD
   (JNIEnv* env, jclass, jdouble size_width, jdouble size_height, jint type, jdouble s_val0, jdouble s_val1, jdouble s_val2, jdouble s_val3)
 {
+    static const char method_name[] = "Mat::n_1Mat__DDIDDDD()";
     try {
-        LOGD("Mat::n_1Mat__DDIDDDD()");
+        LOGD(method_name);
         Size size((int)size_width, (int)size_height);
         Scalar s(s_val0, s_val1, s_val2, s_val3);
         Mat* _retval_ = new Mat( size, type, s );
 
         return (jlong) _retval_;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1Mat__DDIDDDD() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1Mat__DDIDDDD() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1Mat__DDIDDDD()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -165,23 +208,19 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__JIIII
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__JIIII
   (JNIEnv* env, jclass, jlong m_nativeObj, jint rowRange_start, jint rowRange_end, jint colRange_start, jint colRange_end)
 {
+    static const char method_name[] = "Mat::n_1Mat__JIIII()";
     try {
-        LOGD("Mat::n_1Mat__JIIII()");
+        LOGD(method_name);
         Range rowRange(rowRange_start, rowRange_end);
         Range colRange(colRange_start, colRange_end);
         Mat* _retval_ = new Mat( (*(Mat*)m_nativeObj), rowRange, colRange );
 
         return (jlong) _retval_;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1Mat__JIIII() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1Mat__JIIII() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1Mat__JIIII()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -194,22 +233,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__JII
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__JII
   (JNIEnv* env, jclass, jlong m_nativeObj, jint rowRange_start, jint rowRange_end)
 {
+    static const char method_name[] = "Mat::n_1Mat__JII()";
     try {
-        LOGD("Mat::n_1Mat__JII()");
+        LOGD(method_name);
         Range rowRange(rowRange_start, rowRange_end);
         Mat* _retval_ = new Mat( (*(Mat*)m_nativeObj), rowRange );
 
         return (jlong) _retval_;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1Mat__JII() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1Mat__JII() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1Mat__JII()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -225,22 +260,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1adjustROI
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1adjustROI
   (JNIEnv* env, jclass, jlong self, jint dtop, jint dbottom, jint dleft, jint dright)
 {
+    static const char method_name[] = "Mat::n_1adjustROI()";
     try {
-        LOGD("Mat::n_1adjustROI()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Mat _retval_ = me->adjustROI( dtop, dbottom, dleft, dright );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1adjustROI() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1adjustROI() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1adjustROI()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -257,22 +288,18 @@ JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1assignTo__JJI
 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1assignTo__JJI
   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jint type)
 {
+    static const char method_name[] = "Mat::n_1assignTo__JJI()";
     try {
-        LOGD("Mat::n_1assignTo__JJI()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         me->assignTo( (*(Mat*)m_nativeObj), type );
 
         return;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1assignTo__JJI() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return;
     } catch (...) {
-        LOGD("Mat::n_1assignTo__JJI() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1assignTo__JJI()}");
+        throwJavaExceptionD(env, method_name);
         return;
     }
 }
@@ -284,22 +311,18 @@ JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1assignTo__JJ
 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1assignTo__JJ
   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj)
 {
+    static const char method_name[] = "Mat::n_1assignTo__JJ()";
     try {
-        LOGD("Mat::n_1assignTo__JJ()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         me->assignTo( (*(Mat*)m_nativeObj) );
 
         return;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1assignTo__JJ() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return;
     } catch (...) {
-        LOGD("Mat::n_1assignTo__JJ() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1assignTo__JJ()}");
+        throwJavaExceptionD(env, method_name);
         return;
     }
 }
@@ -316,22 +339,18 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1channels
 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1channels
   (JNIEnv* env, jclass, jlong self)
 {
+    static const char method_name[] = "Mat::n_1channels()";
     try {
-        LOGD("Mat::n_1channels()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         int _retval_ = me->channels(  );
 
         return _retval_;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1channels() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1channels() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1channels()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -348,22 +367,18 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1checkVector__JIIZ
 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1checkVector__JIIZ
   (JNIEnv* env, jclass, jlong self, jint elemChannels, jint depth, jboolean requireContinuous)
 {
+    static const char method_name[] = "Mat::n_1checkVector__JIIZ()";
     try {
-        LOGD("Mat::n_1checkVector__JIIZ()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         int _retval_ = me->checkVector( elemChannels, depth, requireContinuous );
 
         return _retval_;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1checkVector__JIIZ() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1checkVector__JIIZ() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1checkVector__JIIZ()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -376,22 +391,18 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1checkVector__JII
 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1checkVector__JII
   (JNIEnv* env, jclass, jlong self, jint elemChannels, jint depth)
 {
+    static const char method_name[] = "Mat::n_1checkVector__JII()";
     try {
-        LOGD("Mat::n_1checkVector__JII()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         int _retval_ = me->checkVector( elemChannels, depth );
 
         return _retval_;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1checkVector__JII() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1checkVector__JII() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1checkVector__JII()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -404,22 +415,18 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1checkVector__JI
 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1checkVector__JI
   (JNIEnv* env, jclass, jlong self, jint elemChannels)
 {
+    static const char method_name[] = "Mat::n_1checkVector__JI()";
     try {
-        LOGD("Mat::n_1checkVector__JI()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         int _retval_ = me->checkVector( elemChannels );
 
         return _retval_;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1checkVector__JI() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1checkVector__JI() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1checkVector__JI()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -437,22 +444,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1clone
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1clone
   (JNIEnv* env, jclass, jlong self)
 {
+    static const char method_name[] = "Mat::n_1clone()";
     try {
-        LOGD("Mat::n_1clone()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Mat _retval_ = me->clone(  );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1clone() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1clone() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1clone()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -469,22 +472,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1col
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1col
   (JNIEnv* env, jclass, jlong self, jint x)
 {
+    static const char method_name[] = "Mat::n_1col()";
     try {
-        LOGD("Mat::n_1col()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Mat _retval_ = me->col( x );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1col() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1col() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1col()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -501,22 +500,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1colRange
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1colRange
   (JNIEnv* env, jclass, jlong self, jint startcol, jint endcol)
 {
+    static const char method_name[] = "Mat::n_1colRange()";
     try {
-        LOGD("Mat::n_1colRange()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Mat _retval_ = me->colRange( startcol, endcol );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1colRange() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1colRange() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1colRange()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -526,34 +521,31 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1colRange
 //
 //  int Mat::dims()
 //
-
+  
 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1dims
   (JNIEnv* env, jclass, jlong self);
 
 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1dims
   (JNIEnv* env, jclass, jlong self)
 {
+    static const char method_name[] = "Mat::n_1dims()";
     try {
-        LOGD("Mat::n_1dims()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         int _retval_ = me->dims;
 
         return _retval_;
     } catch(cv::Exception e) {
-        LOGD("Mat::n_1dims() catched cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1dims() catched unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1dims()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
 
 
+
 //
 //  int Mat::cols()
 //
@@ -564,22 +556,18 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1cols
 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1cols
   (JNIEnv* env, jclass, jlong self)
 {
+    static const char method_name[] = "Mat::n_1cols()";
     try {
-        LOGD("Mat::n_1cols()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         int _retval_ = me->cols;
 
         return _retval_;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1cols() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1cols() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1cols()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -596,23 +584,19 @@ JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1convertTo__JJIDD
 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1convertTo__JJIDD
   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jint rtype, jdouble alpha, jdouble beta)
 {
+    static const char method_name[] = "Mat::n_1convertTo__JJIDD()";
     try {
-        LOGD("Mat::n_1convertTo__JJIDD()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Mat& m = *((Mat*)m_nativeObj);
         me->convertTo( m, rtype, alpha, beta );
 
         return;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1convertTo__JJIDD() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return;
     } catch (...) {
-        LOGD("Mat::n_1convertTo__JJIDD() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1convertTo__JJIDD()}");
+        throwJavaExceptionD(env, method_name);
         return;
     }
 }
@@ -624,23 +608,19 @@ JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1convertTo__JJID
 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1convertTo__JJID
   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jint rtype, jdouble alpha)
 {
+    static const char method_name[] = "Mat::n_1convertTo__JJID()";
     try {
-        LOGD("Mat::n_1convertTo__JJID()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Mat& m = *((Mat*)m_nativeObj);
         me->convertTo( m, rtype, alpha );
 
         return;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1convertTo__JJID() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return;
     } catch (...) {
-        LOGD("Mat::n_1convertTo__JJID() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1convertTo__JJID()}");
+        throwJavaExceptionD(env, method_name);
         return;
     }
 }
@@ -652,23 +632,19 @@ JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1convertTo__JJI
 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1convertTo__JJI
   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jint rtype)
 {
+    static const char method_name[] = "Mat::n_1convertTo__JJI()";
     try {
-        LOGD("Mat::n_1convertTo__JJI()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Mat& m = *((Mat*)m_nativeObj);
         me->convertTo( m, rtype );
 
         return;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1convertTo__JJI() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return;
     } catch (...) {
-        LOGD("Mat::n_1convertTo__JJI() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1convertTo__JJI()}");
+        throwJavaExceptionD(env, method_name);
         return;
     }
 }
@@ -685,23 +661,19 @@ JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1copyTo__JJ
 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1copyTo__JJ
   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj)
 {
+    static const char method_name[] = "Mat::n_1copyTo__JJ()";
     try {
-        LOGD("Mat::n_1copyTo__JJ()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Mat& m = *((Mat*)m_nativeObj);
         me->copyTo( m );
 
         return;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1copyTo__JJ() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return;
     } catch (...) {
-        LOGD("Mat::n_1copyTo__JJ() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1copyTo__JJ()}");
+        throwJavaExceptionD(env, method_name);
         return;
     }
 }
@@ -718,24 +690,20 @@ JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1copyTo__JJJ
 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1copyTo__JJJ
   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jlong mask_nativeObj)
 {
+    static const char method_name[] = "Mat::n_1copyTo__JJJ()";
     try {
-        LOGD("Mat::n_1copyTo__JJJ()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Mat& m = *((Mat*)m_nativeObj);
         Mat& mask = *((Mat*)mask_nativeObj);
         me->copyTo( m, mask );
 
         return;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1copyTo__JJJ() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return;
     } catch (...) {
-        LOGD("Mat::n_1copyTo__JJJ() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1copyTo__JJJ()}");
+        throwJavaExceptionD(env, method_name);
         return;
     }
 }
@@ -752,22 +720,18 @@ JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1create__JIII
 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1create__JIII
   (JNIEnv* env, jclass, jlong self, jint rows, jint cols, jint type)
 {
+    static const char method_name[] = "Mat::n_1create__JIII()";
     try {
-        LOGD("Mat::n_1create__JIII()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         me->create( rows, cols, type );
 
         return;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1create__JIII() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return;
     } catch (...) {
-        LOGD("Mat::n_1create__JIII() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1create__JIII()}");
+        throwJavaExceptionD(env, method_name);
         return;
     }
 }
@@ -784,23 +748,19 @@ JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1create__JDDI
 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1create__JDDI
   (JNIEnv* env, jclass, jlong self, jdouble size_width, jdouble size_height, jint type)
 {
+    static const char method_name[] = "Mat::n_1create__JDDI()";
     try {
-        LOGD("Mat::n_1create__JDDI()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Size size((int)size_width, (int)size_height);
         me->create( size, type );
 
         return;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1create__JDDI() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return;
     } catch (...) {
-        LOGD("Mat::n_1create__JDDI() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1create__JDDI()}");
+        throwJavaExceptionD(env, method_name);
         return;
     }
 }
@@ -817,23 +777,19 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1cross
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1cross
   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj)
 {
+    static const char method_name[] = "Mat::n_1cross()";
     try {
-        LOGD("Mat::n_1cross()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Mat& m = *((Mat*)m_nativeObj);
         Mat _retval_ = me->cross( m );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1cross() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1cross() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1cross()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -867,22 +823,18 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1depth
 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1depth
   (JNIEnv* env, jclass, jlong self)
 {
+    static const char method_name[] = "Mat::n_1depth()";
     try {
-        LOGD("Mat::n_1depth()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         int _retval_ = me->depth(  );
 
         return _retval_;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1depth() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1depth() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1depth()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -899,22 +851,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1diag__JI
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1diag__JI
   (JNIEnv* env, jclass, jlong self, jint d)
 {
+    static const char method_name[] = "Mat::n_1diag__JI()";
     try {
-        LOGD("Mat::n_1diag__JI()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Mat _retval_ = me->diag( d );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1diag__JI() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1diag__JI() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1diag__JI()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -932,22 +880,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1diag__J
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1diag__J
   (JNIEnv* env, jclass, jlong d_nativeObj)
 {
+    static const char method_name[] = "Mat::n_1diag__J()";
     try {
-        LOGD("Mat::n_1diag__J()");
+        LOGD(method_name);
 
         Mat _retval_ = Mat::diag( (*(Mat*)d_nativeObj) );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1diag__J() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1diag__J() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1diag__J()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -964,23 +908,19 @@ JNIEXPORT jdouble JNICALL Java_org_opencv_core_Mat_n_1dot
 JNIEXPORT jdouble JNICALL Java_org_opencv_core_Mat_n_1dot
   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj)
 {
+    static const char method_name[] = "Mat::n_1dot()";
     try {
-        LOGD("Mat::n_1dot()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Mat& m = *((Mat*)m_nativeObj);
         double _retval_ = me->dot( m );
 
         return _retval_;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1dot() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1dot() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1dot()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -997,22 +937,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1elemSize
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1elemSize
   (JNIEnv* env, jclass, jlong self)
 {
+    static const char method_name[] = "Mat::n_1elemSize()";
     try {
-        LOGD("Mat::n_1elemSize()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         size_t _retval_ = me->elemSize(  );
 
         return _retval_;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1elemSize() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1elemSize() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1elemSize()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1029,22 +965,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1elemSize1
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1elemSize1
   (JNIEnv* env, jclass, jlong self)
 {
+    static const char method_name[] = "Mat::n_1elemSize1()";
     try {
-        LOGD("Mat::n_1elemSize1()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         size_t _retval_ = me->elemSize1(  );
 
         return _retval_;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1elemSize1() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1elemSize1() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1elemSize1()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1061,22 +993,18 @@ JNIEXPORT jboolean JNICALL Java_org_opencv_core_Mat_n_1empty
 JNIEXPORT jboolean JNICALL Java_org_opencv_core_Mat_n_1empty
   (JNIEnv* env, jclass, jlong self)
 {
+    static const char method_name[] = "Mat::n_1empty()";
     try {
-        LOGD("Mat::n_1empty()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         bool _retval_ = me->empty(  );
 
         return _retval_;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1empty() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1empty() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1empty()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1093,22 +1021,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1eye__III
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1eye__III
   (JNIEnv* env, jclass, jint rows, jint cols, jint type)
 {
+    static const char method_name[] = "Mat::n_1eye__III()";
     try {
-        LOGD("Mat::n_1eye__III()");
+        LOGD(method_name);
 
         Mat _retval_ = Mat::eye( rows, cols, type );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1eye__III() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1eye__III() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1eye__III()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1125,22 +1049,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1eye__DDI
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1eye__DDI
   (JNIEnv* env, jclass, jdouble size_width, jdouble size_height, jint type)
 {
+    static const char method_name[] = "Mat::n_1eye__DDI()";
     try {
-        LOGD("Mat::n_1eye__DDI()");
+        LOGD(method_name);
         Size size((int)size_width, (int)size_height);
         Mat _retval_ = Mat::eye( size, type );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1eye__DDI() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1eye__DDI() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1eye__DDI()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1157,22 +1077,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1inv__JI
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1inv__JI
   (JNIEnv* env, jclass, jlong self, jint method)
 {
+    static const char method_name[] = "Mat::n_1inv__JI()";
     try {
-        LOGD("Mat::n_1inv__JI()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Mat _retval_ = me->inv( method );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1inv__JI() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1inv__JI() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1inv__JI()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1184,22 +1100,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1inv__J
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1inv__J
   (JNIEnv* env, jclass, jlong self)
 {
+    static const char method_name[] = "Mat::n_1inv__J()";
     try {
-        LOGD("Mat::n_1inv__J()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Mat _retval_ = me->inv(  );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1inv__J() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1inv__J() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1inv__J()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1216,22 +1128,18 @@ JNIEXPORT jboolean JNICALL Java_org_opencv_core_Mat_n_1isContinuous
 JNIEXPORT jboolean JNICALL Java_org_opencv_core_Mat_n_1isContinuous
   (JNIEnv* env, jclass, jlong self)
 {
+    static const char method_name[] = "Mat::n_1isContinuous()";
     try {
-        LOGD("Mat::n_1isContinuous()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         bool _retval_ = me->isContinuous(  );
 
         return _retval_;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1isContinuous() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1isContinuous() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1isContinuous()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1248,22 +1156,18 @@ JNIEXPORT jboolean JNICALL Java_org_opencv_core_Mat_n_1isSubmatrix
 JNIEXPORT jboolean JNICALL Java_org_opencv_core_Mat_n_1isSubmatrix
   (JNIEnv* env, jclass, jlong self)
 {
+    static const char method_name[] = "Mat::n_1isSubmatrix()";
     try {
-        LOGD("Mat::n_1isSubmatrix()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         bool _retval_ = me->isSubmatrix(  );
 
         return _retval_;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1isSubmatrix() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1isSubmatrix() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1isSubmatrix()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1280,24 +1184,20 @@ JNIEXPORT void JNICALL Java_org_opencv_core_Mat_locateROI_10
 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_locateROI_10
   (JNIEnv* env, jclass, jlong self, jdoubleArray wholeSize_out, jdoubleArray ofs_out)
 {
+    static const char method_name[] = "core::locateROI_10()";
     try {
-        LOGD("core::locateROI_10()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Size wholeSize;
         Point ofs;
         me->locateROI( wholeSize, ofs );
         jdouble tmp_wholeSize[2] = {wholeSize.width, wholeSize.height}; env->SetDoubleArrayRegion(wholeSize_out, 0, 2, tmp_wholeSize);  jdouble tmp_ofs[2] = {ofs.x, ofs.y}; env->SetDoubleArrayRegion(ofs_out, 0, 2, tmp_ofs);
         return;
-    } catch(cv::Exception e) {
-        LOGD("Mat::locateROI_10() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return;
     } catch (...) {
-        LOGD("Mat::locateROI_10() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::locateROI_10()}");
+        throwJavaExceptionD(env, method_name);
         return;
     }
 }
@@ -1314,23 +1214,19 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1mul__JJD
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1mul__JJD
   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jdouble scale)
 {
+    static const char method_name[] = "Mat::n_1mul__JJD()";
     try {
-        LOGD("Mat::n_1mul__JJD()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Mat& m = *((Mat*)m_nativeObj);
         Mat _retval_ = me->mul( m, scale );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1mul__JJD() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1mul__JJD() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1mul__JJD()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1343,23 +1239,19 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1mul__JJ
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1mul__JJ
   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj)
 {
+    static const char method_name[] = "Mat::n_1mul__JJ()";
     try {
-        LOGD("Mat::n_1mul__JJ()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Mat& m = *((Mat*)m_nativeObj);
         Mat _retval_ = me->mul( m );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1mul__JJ() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1mul__JJ() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1mul__JJ()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1376,22 +1268,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1ones__III
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1ones__III
   (JNIEnv* env, jclass, jint rows, jint cols, jint type)
 {
+    static const char method_name[] = "Mat::n_1ones__III()";
     try {
-        LOGD("Mat::n_1ones__III()");
+        LOGD(method_name);
 
         Mat _retval_ = Mat::ones( rows, cols, type );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1ones__III() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1ones__III() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1ones__III()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1408,22 +1296,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1ones__DDI
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1ones__DDI
   (JNIEnv* env, jclass, jdouble size_width, jdouble size_height, jint type)
 {
+    static const char method_name[] = "Mat::n_1ones__DDI()";
     try {
-        LOGD("Mat::n_1ones__DDI()");
+        LOGD(method_name);
         Size size((int)size_width, (int)size_height);
         Mat _retval_ = Mat::ones( size, type );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1ones__DDI() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1ones__DDI() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1ones__DDI()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1440,22 +1324,18 @@ JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1push_1back
 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1push_1back
   (JNIEnv* env, jclass, jlong self, jlong m_nativeObj)
 {
+    static const char method_name[] = "Mat::n_1push_1back()";
     try {
-        LOGD("Mat::n_1push_1back()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         me->push_back( (*(Mat*)m_nativeObj) );
 
         return;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1push_1back() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return;
     } catch (...) {
-        LOGD("Mat::n_1push_1back() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1push_1back()}");
+        throwJavaExceptionD(env, method_name);
         return;
     }
 }
@@ -1472,22 +1352,18 @@ JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1release
 JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1release
   (JNIEnv* env, jclass, jlong self)
 {
+    static const char method_name[] = "Mat::n_1release()";
     try {
-        LOGD("Mat::n_1release()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         me->release(  );
 
         return;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1release() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return;
     } catch (...) {
-        LOGD("Mat::n_1release() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1release()}");
+        throwJavaExceptionD(env, method_name);
         return;
     }
 }
@@ -1504,22 +1380,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1reshape__JII
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1reshape__JII
   (JNIEnv* env, jclass, jlong self, jint cn, jint rows)
 {
+    static const char method_name[] = "Mat::n_1reshape__JII()";
     try {
-        LOGD("Mat::n_1reshape__JII()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Mat _retval_ = me->reshape( cn, rows );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1reshape__JII() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1reshape__JII() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1reshape__JII()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1532,22 +1404,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1reshape__JI
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1reshape__JI
   (JNIEnv* env, jclass, jlong self, jint cn)
 {
+    static const char method_name[] = "Mat::n_1reshape__JI()";
     try {
-        LOGD("Mat::n_1reshape__JI()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Mat _retval_ = me->reshape( cn );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1reshape__JI() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1reshape__JI() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1reshape__JI()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1564,22 +1432,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1row
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1row
   (JNIEnv* env, jclass, jlong self, jint y)
 {
+    static const char method_name[] = "Mat::n_1row()";
     try {
-        LOGD("Mat::n_1row()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Mat _retval_ = me->row( y );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1row() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1row() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1row()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1596,22 +1460,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1rowRange
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1rowRange
   (JNIEnv* env, jclass, jlong self, jint startrow, jint endrow)
 {
+    static const char method_name[] = "Mat::n_1rowRange()";
     try {
-        LOGD("Mat::n_1rowRange()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Mat _retval_ = me->rowRange( startrow, endrow );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1rowRange() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1rowRange() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1rowRange()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1628,22 +1488,18 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1rows
 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1rows
   (JNIEnv* env, jclass, jlong self)
 {
+    static const char method_name[] = "Mat::n_1rows()";
     try {
-        LOGD("Mat::n_1rows()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         int _retval_ = me->rows;
 
         return _retval_;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1rows() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1rows() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1rows()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1660,23 +1516,19 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1setTo__JDDDD
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1setTo__JDDDD
   (JNIEnv* env, jclass, jlong self, jdouble s_val0, jdouble s_val1, jdouble s_val2, jdouble s_val3)
 {
+    static const char method_name[] = "Mat::n_1setTo__JDDDD()";
     try {
-        LOGD("Mat::n_1setTo__JDDDD()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Scalar s(s_val0, s_val1, s_val2, s_val3);
         Mat _retval_ = me->operator =( s );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1setTo__JDDDD() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1setTo__JDDDD() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1setTo__JDDDD()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1693,24 +1545,20 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1setTo__JDDDDJ
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1setTo__JDDDDJ
   (JNIEnv* env, jclass, jlong self, jdouble s_val0, jdouble s_val1, jdouble s_val2, jdouble s_val3, jlong mask_nativeObj)
 {
+    static const char method_name[] = "Mat::n_1setTo__JDDDDJ()";
     try {
-        LOGD("Mat::n_1setTo__JDDDDJ()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Scalar s(s_val0, s_val1, s_val2, s_val3);
         Mat& mask = *((Mat*)mask_nativeObj);
         Mat _retval_ = me->setTo( s, mask );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1setTo__JDDDDJ() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1setTo__JDDDDJ() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1setTo__JDDDDJ()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1727,24 +1575,20 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1setTo__JJJ
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1setTo__JJJ
   (JNIEnv* env, jclass, jlong self, jlong value_nativeObj, jlong mask_nativeObj)
 {
+    static const char method_name[] = "Mat::n_1setTo__JJJ()";
     try {
-        LOGD("Mat::n_1setTo__JJJ()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Mat& value = *((Mat*)value_nativeObj);
         Mat& mask = *((Mat*)mask_nativeObj);
         Mat _retval_ = me->setTo( value, mask );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1setTo__JJJ() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1setTo__JJJ() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1setTo__JJJ()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1757,23 +1601,19 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1setTo__JJ
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1setTo__JJ
   (JNIEnv* env, jclass, jlong self, jlong value_nativeObj)
 {
+    static const char method_name[] = "Mat::n_1setTo__JJ()";
     try {
-        LOGD("Mat::n_1setTo__JJ()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Mat& value = *((Mat*)value_nativeObj);
         Mat _retval_ = me->setTo( value );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1setTo__JJ() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1setTo__JJ() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1setTo__JJ()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1790,22 +1630,18 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_Mat_n_1size
 JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_Mat_n_1size
   (JNIEnv* env, jclass, jlong self)
 {
+    static const char method_name[] = "Mat::n_1size()";
     try {
-        LOGD("Mat::n_1size()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Size _retval_ = me->size(  );
         jdoubleArray _da_retval_ = env->NewDoubleArray(2);  jdouble _tmp_retval_[2] = {_retval_.width, _retval_.height}; env->SetDoubleArrayRegion(_da_retval_, 0, 2, _tmp_retval_);
         return _da_retval_;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1size() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1size() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1size()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1822,22 +1658,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1step1__JI
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1step1__JI
   (JNIEnv* env, jclass, jlong self, jint i)
 {
+    static const char method_name[] = "Mat::n_1step1__JI()";
     try {
-        LOGD("Mat::n_1step1__JI()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         size_t _retval_ = me->step1( i );
 
         return _retval_;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1step1__JI() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1step1__JI() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1step1__JI()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1850,22 +1682,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1step1__J
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1step1__J
   (JNIEnv* env, jclass, jlong self)
 {
+    static const char method_name[] = "Mat::n_1step1__J()";
     try {
-        LOGD("Mat::n_1step1__J()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         size_t _retval_ = me->step1(  );
 
         return _retval_;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1step1__J() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1step1__J() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1step1__J()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1880,24 +1708,20 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1submat_1rr
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1submat_1rr
   (JNIEnv* env, jclass, jlong self, jint rowRange_start, jint rowRange_end, jint colRange_start, jint colRange_end)
 {
+    static const char method_name[] = "Mat::n_1submat_1rr()";
     try {
-        LOGD("Mat::n_1submat_1rr()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Range rowRange(rowRange_start, rowRange_end);
         Range colRange(colRange_start, colRange_end);
         Mat _retval_ = me->operator()( rowRange, colRange );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1submat_1rr() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1submat_1rr() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1submat_1rr()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1914,23 +1738,19 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1submat
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1submat
   (JNIEnv* env, jclass, jlong self, jint roi_x, jint roi_y, jint roi_width, jint roi_height)
 {
+    static const char method_name[] = "Mat::n_1submat()";
     try {
-        LOGD("Mat::n_1submat()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Rect roi(roi_x, roi_y, roi_width, roi_height);
         Mat _retval_ = me->operator()( roi );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1submat() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1submat() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1submat()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1947,22 +1767,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1t
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1t
   (JNIEnv* env, jclass, jlong self)
 {
+    static const char method_name[] = "Mat::n_1t()";
     try {
-        LOGD("Mat::n_1t()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         Mat _retval_ = me->t(  );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1t() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1t() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1t()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -1979,22 +1795,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1total
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1total
   (JNIEnv* env, jclass, jlong self)
 {
+    static const char method_name[] = "Mat::n_1total()";
     try {
-        LOGD("Mat::n_1total()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         size_t _retval_ = me->total(  );
 
         return _retval_;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1total() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1total() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1total()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -2011,22 +1823,18 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1type
 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1type
   (JNIEnv* env, jclass, jlong self)
 {
+    static const char method_name[] = "Mat::n_1type()";
     try {
-        LOGD("Mat::n_1type()");
+        LOGD(method_name);
         Mat* me = (Mat*) self; //TODO: check for NULL
         int _retval_ = me->type(  );
 
         return _retval_;
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1type() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1type() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1type()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -2043,22 +1851,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1zeros__III
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1zeros__III
   (JNIEnv* env, jclass, jint rows, jint cols, jint type)
 {
+    static const char method_name[] = "Mat::n_1zeros__III()";
     try {
-        LOGD("Mat::n_1zeros__III()");
+        LOGD(method_name);
 
         Mat _retval_ = Mat::zeros( rows, cols, type );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1zeros__III() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1zeros__III() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1zeros__III()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -2075,22 +1879,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1zeros__DDI
 JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1zeros__DDI
   (JNIEnv* env, jclass, jdouble size_width, jdouble size_height, jint type)
 {
+    static const char method_name[] = "Mat::n_1zeros__DDI()";
     try {
-        LOGD("Mat::n_1zeros__DDI()");
+        LOGD(method_name);
         Size size((int)size_width, (int)size_height);
         Mat _retval_ = Mat::zeros( size, type );
 
         return (jlong) new Mat(_retval_);
-    } catch(cv::Exception e) {
-        LOGD("Mat::n_1zeros__DDI() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::n_1zeros__DDI() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::n_1zeros__DDI()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -2120,8 +1920,9 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutD
 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutD
     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jdoubleArray vals)
 {
+    static const char method_name[] = "Mat::nPutD()";
     try {
-        LOGD("Mat::nPutD()");
+        LOGD(method_name);
         cv::Mat* me = (cv::Mat*) self;
         if(!me || !me->data) return 0;  // no native object behind
         if(me->rows<=row || me->cols<=col) return 0; // indexes out of range
@@ -2161,16 +1962,11 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutD
 
         env->ReleasePrimitiveArrayCritical(vals, values, 0);
         return res;
-    } catch(cv::Exception e) {
-        LOGD("Mat::nPutD() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::nPutD() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::nPutD()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -2217,8 +2013,9 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutB
 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutB
     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jbyteArray vals)
 {
+    static const char method_name[] = "Mat::nPutB()";
     try {
-        LOGD("Mat::nPutB()");
+        LOGD(method_name);
         cv::Mat* me = (cv::Mat*) self;
         if(! self) return 0; // no native object behind
         if(me->depth() != CV_8U && me->depth() != CV_8S) return 0; // incompatible type
@@ -2228,16 +2025,11 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutB
         int res = mat_put<char>(me, row, col, count, values);
         env->ReleasePrimitiveArrayCritical(vals, values, 0);
         return res;
-    } catch(cv::Exception e) {
-        LOGD("Mat::nPutB() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::nPutB() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::nPutB()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -2248,8 +2040,9 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutS
 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutS
     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jshortArray vals)
 {
+    static const char method_name[] = "Mat::nPutS()";
     try {
-        LOGD("Mat::nPutS()");
+        LOGD(method_name);
         cv::Mat* me = (cv::Mat*) self;
         if(! self) return 0; // no native object behind
         if(me->depth() != CV_16U && me->depth() != CV_16S) return 0; // incompatible type
@@ -2259,16 +2052,11 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutS
         int res = mat_put<short>(me, row, col, count, values);
         env->ReleasePrimitiveArrayCritical(vals, values, 0);
         return res;
-    } catch(cv::Exception e) {
-        LOGD("Mat::nPutS() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::nPutS() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::nPutS()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -2279,8 +2067,9 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutI
 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutI
     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jintArray vals)
 {
+    static const char method_name[] = "Mat::nPutI()";
     try {
-        LOGD("Mat::nPutI()");
+        LOGD(method_name);
         cv::Mat* me = (cv::Mat*) self;
         if(! self) return 0; // no native object behind
         if(me->depth() != CV_32S) return 0; // incompatible type
@@ -2290,16 +2079,11 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutI
         int res = mat_put<int>(me, row, col, count, values);
         env->ReleasePrimitiveArrayCritical(vals, values, 0);
         return res;
-    } catch(cv::Exception e) {
-        LOGD("Mat::nPutI() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::nPutI() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::nPutI()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -2310,8 +2094,9 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutF
 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutF
     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jfloatArray vals)
 {
+    static const char method_name[] = "Mat::nPutF()";
     try {
-        LOGD("Mat::nPutF()");
+        LOGD(method_name);
         cv::Mat* me = (cv::Mat*) self;
         if(! self) return 0; // no native object behind
         if(me->depth() != CV_32F) return 0; // incompatible type
@@ -2321,16 +2106,11 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutF
         int res = mat_put<float>(me, row, col, count, values);
         env->ReleasePrimitiveArrayCritical(vals, values, 0);
         return res;
-    } catch(cv::Exception e) {
-        LOGD("Mat::nPutF() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::nPutF() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::nPutF()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -2376,8 +2156,9 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetB
 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetB
     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jbyteArray vals)
 {
+    static const char method_name[] = "Mat::nGetB()";
     try {
-        LOGD("Mat::nGetB()");
+        LOGD(method_name);
         cv::Mat* me = (cv::Mat*) self;
         if(! self) return 0; // no native object behind
         if(me->depth() != CV_8U && me->depth() != CV_8S) return 0; // incompatible type
@@ -2387,16 +2168,11 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetB
         int res = mat_get<char>(me, row, col, count, values);
         env->ReleasePrimitiveArrayCritical(vals, values, 0);
         return res;
-    } catch(cv::Exception e) {
-        LOGD("Mat::nGetB() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::nGetB() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::nGetB()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -2407,8 +2183,9 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetS
 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetS
     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jshortArray vals)
 {
+    static const char method_name[] = "Mat::nGetS()";
     try {
-        LOGD("Mat::nGetS()");
+        LOGD(method_name);
         cv::Mat* me = (cv::Mat*) self;
         if(! self) return 0; // no native object behind
         if(me->depth() != CV_16U && me->depth() != CV_16S) return 0; // incompatible type
@@ -2418,16 +2195,11 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetS
         int res = mat_get<short>(me, row, col, count, values);
         env->ReleasePrimitiveArrayCritical(vals, values, 0);
         return res;
-    } catch(cv::Exception e) {
-        LOGD("Mat::nGetS() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::nGetS() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::nGetS()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -2438,8 +2210,9 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetI
 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetI
     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jintArray vals)
 {
+    static const char method_name[] = "Mat::nGetI()";
     try {
-        LOGD("Mat::nGetI()");
+        LOGD(method_name);
         cv::Mat* me = (cv::Mat*) self;
         if(! self) return 0; // no native object behind
         if(me->depth() != CV_32S) return 0; // incompatible type
@@ -2449,16 +2222,11 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetI
         int res = mat_get<int>(me, row, col, count, values);
         env->ReleasePrimitiveArrayCritical(vals, values, 0);
         return res;
-    } catch(cv::Exception e) {
-        LOGD("Mat::nGetI() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::nGetI() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::nGetI()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -2469,8 +2237,9 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetF
 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetF
     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jfloatArray vals)
 {
+    static const char method_name[] = "Mat::nGetF()";
     try {
-        LOGD("Mat::nGetF()");
+        LOGD(method_name);
         cv::Mat* me = (cv::Mat*) self;
         if(! self) return 0; // no native object behind
         if(me->depth() != CV_32F) return 0; // incompatible type
@@ -2480,16 +2249,11 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetF
         int res = mat_get<float>(me, row, col, count, values);
         env->ReleasePrimitiveArrayCritical(vals, values, 0);
         return res;
-    } catch(cv::Exception e) {
-        LOGD("Mat::nGetF() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::nGetF() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::nGetF()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -2500,8 +2264,9 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetD
 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetD
     (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jdoubleArray vals)
 {
+    static const char method_name[] = "Mat::nGetD()";
     try {
-        LOGD("Mat::nGetD()");
+        LOGD(method_name);
         cv::Mat* me = (cv::Mat*) self;
         if(! self) return 0; // no native object behind
         if(me->depth() != CV_64F) return 0; // incompatible type
@@ -2511,16 +2276,11 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetD
         int res = mat_get<double>(me, row, col, count, values);
         env->ReleasePrimitiveArrayCritical(vals, values, 0);
         return res;
-    } catch(cv::Exception e) {
-        LOGD("Mat::nGetD() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::nGetD() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::nGetD()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -2531,8 +2291,9 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_Mat_nGet
 JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_Mat_nGet
     (JNIEnv* env, jclass, jlong self, jint row, jint col)
 {
+    static const char method_name[] = "Mat::nGet()";
     try {
-        LOGD("Mat::nGet()");
+        LOGD(method_name);
         cv::Mat* me = (cv::Mat*) self;
         if(! self) return 0; // no native object behind
         if(me->rows<=row || me->cols<=col) return 0; // indexes out of range
@@ -2553,16 +2314,11 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_Mat_nGet
             env->SetDoubleArrayRegion(res, 0, me->channels(), buff);
         }
         return res;
-    } catch(cv::Exception e) {
-        LOGD("Mat::nGet() caught cv::Exception: %s", e.what());
-        jclass je = env->FindClass("org/opencv/core/CvException");
-        if(!je) je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, e.what());
+    } catch(const std::exception &e) {
+        throwJavaExceptionD(env, e, method_name);
         return 0;
     } catch (...) {
-        LOGD("Mat::nGet() caught unknown exception (...)");
-        jclass je = env->FindClass("java/lang/Exception");
-        env->ThrowNew(je, "Unknown exception in JNI code {Mat::nGet()}");
+        throwJavaExceptionD(env, method_name);
         return 0;
     }
 }
@@ -2573,25 +2329,21 @@ JNIEXPORT jstring JNICALL Java_org_opencv_core_Mat_nDump
 JNIEXPORT jstring JNICALL Java_org_opencv_core_Mat_nDump
   (JNIEnv *env, jclass, jlong self)
 {
-    cv::Mat* me = (cv::Mat*) self; //TODO: check for NULL
-    std::stringstream s;
+    static const char method_name[] = "Mat::nDump()";
     try {
-            LOGD("Mat::nDump()");
-
-            s << *me;
-            return env->NewStringUTF(s.str().c_str());
-        } catch(cv::Exception e) {
-            LOGE("Mat::nDump() caught cv::Exception: %s", e.what());
-            jclass je = env->FindClass("org/opencv/core/CvException");
-            if(!je) je = env->FindClass("java/lang/Exception");
-            env->ThrowNew(je, e.what());
-            return env->NewStringUTF("ERROR");
-        } catch (...) {
-            LOGE("Mat::nDump() caught unknown exception (...)");
-            jclass je = env->FindClass("java/lang/Exception");
-            env->ThrowNew(je, "Unknown exception in JNI code {Mat::nDump()}");
-            return env->NewStringUTF("ERROR");
-        }
+        LOGD(method_name);
+        cv::Mat* me = (cv::Mat*) self; //TODO: check for NULL
+        std::stringstream s;
+        s << *me;
+        std::string str = s.str();
+        return env->NewStringUTF(str.c_str());
+    } catch(const std::exception &e) {
+        throwJavaExceptionE(env, e, method_name);
+        return 0;
+    } catch (...) {
+        throwJavaExceptionE(env, method_name);
+        return 0;
+    }
 }