{"action":"created","comment":{"url":"https://api.github.com/repos/realm/realm-java/pulls/comments/82737988","pull_request_review_id":3622825,"id":82737988,"diff_hunk":"@@ -58,11 +61,73 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved);\n #define STRINGIZE_DETAIL(x) #x\n #define STRINGIZE(x) STRINGIZE_DETAIL(x)\n \n-// Exception handling\n-#define CATCH_STD() \\\n-    catch (...) { \\\n-        ConvertException(env, __FILE__, __LINE__); \\\n+void ConvertException(JNIEnv* env);\n+\n+// Exception handling using lambdas\n+// Credit: http://stackoverflow.com/questions/29202377/how-do-i-force-the-compiler-to-recognize-lambda-type-in-templated-function\n+template<typename T, typename F>\n+T try_catch(JNIEnv* env, F func) {\n+    try {\n+        // if an exception already has occurred, it is not possible to call any JNI\n+        // functions, and we don't wish to try to execute the JNI function\n+        // alternatively, we could clear the exception but then we can easily miss\n+        // an error stage\n+        if (env->ExceptionCheck() == JNI_FALSE) {\n+            // the actual JNI code is executed\n+            // it might throw C++ exceptions\n+            return func();\n+        }\n     }\n+    catch (std::exception& e) {\n+        // we don't wish to do the exception handling in here; this is a templated function, and we might end up\n+        // with multiple copies of the code for no good reason\n+        ConvertException(env);\n+    }\n+\n+    // when a Java exception is thrown, we return to Java with any return value; as soon as the execution point is\n+    // back at the JVM, the Java exception is thrown, and the returned value isn't used for anything\n+    return static_cast<T>(0);\n+}\n+\n+// we need to have specialized C++ exceptions so we know what kind of error state has been reached\n+// illegal_state - the internal state of the Realm is wrong; it is not serious but execution cannot continue\n+class illegal_state : public std::runtime_error {\n+public:\n+    illegal_state(const std::string& msg) : std::runtime_error(msg) {};\n+    illegal_state(const char* msg) : std::runtime_error(msg) {};\n+};\n+\n+// unsupported_operation - the operation is either not supported or implemented; see also java.lang.UnsupportedOperationException\n+class unsupported_operation : public std::runtime_error {\n+public:\n+    unsupported_operation(const std::string& msg) : std::runtime_error(msg) {};\n+    unsupported_operation(const char* msg) : std::runtime_error(msg) {};\n+};\n+\n+// fatal_error - the app cannot recover from its error state\n+class fatal_error : public std::runtime_error {\n+public:\n+    fatal_error(const std::string& msg) : std::runtime_error(msg) {};\n+    fatal_error(const char* msg) : std::runtime_error(msg) {};\n+};\n+\n+// class_not_found - looking on a class in JVM failed; see also java.lang.ClassNotFoundException\n+class class_not_found : public std::runtime_error {\n+public:\n+    class_not_found(const std::string& msg) : std::runtime_error(msg) {};\n+    class_not_found(const char* msg) : std::runtime_error(msg) {};\n+};\n+\n+// null_value - trying to set a required field to null\n+class null_value : public std::exception {","path":"realm/realm-library/src/main/cpp/util.hpp","position":76,"original_position":76,"commit_id":"39d846308b488078baf95787d009f74f0b90b81d","original_commit_id":"39d846308b488078baf95787d009f74f0b90b81d","user":{"login":"beeender","id":449296,"avatar_url":"https://avatars.githubusercontent.com/u/449296?v=3","gravatar_id":"","url":"https://api.github.com/users/beeender","html_url":"https://github.com/beeender","followers_url":"https://api.github.com/users/beeender/followers","following_url":"https://api.github.com/users/beeender/following{/other_user}","gists_url":"https://api.github.com/users/beeender/gists{/gist_id}","starred_url":"https://api.github.com/users/beeender/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/beeender/subscriptions","organizations_url":"https://api.github.com/users/beeender/orgs","repos_url":"https://api.github.com/users/beeender/repos","events_url":"https://api.github.com/users/beeender/events{/privacy}","received_events_url":"https://api.github.com/users/beeender/received_events","type":"User","site_admin":false},"body":"why `exception` instead of `runtime_error` here?","created_at":"2016-10-11T07:39:27Z","updated_at":"2016-10-11T07:48:22Z","html_url":"https://github.com/realm/realm-java/pull/3602#discussion_r82737988","pull_request_url":"https://api.github.com/repos/realm/realm-java/pulls/3602","_links":{"self":{"href":"https://api.github.com/repos/realm/realm-java/pulls/comments/82737988"},"html":{"href":"https://github.com/realm/realm-java/pull/3602#discussion_r82737988"},"pull_request":{"href":"https://api.github.com/repos/realm/realm-java/pulls/3602"}}},"pull_request":{"url":"https://api.github.com/repos/realm/realm-java/pulls/3602","id":88682835,"html_url":"https://github.com/realm/realm-java/pull/3602","diff_url":"https://github.com/realm/realm-java/pull/3602.diff","patch_url":"https://github.com/realm/realm-java/pull/3602.patch","issue_url":"https://api.github.com/repos/realm/realm-java/issues/3602","number":3602,"state":"open","locked":false,"title":"Modern exception handling","user":{"login":"kneth","id":1225363,"avatar_url":"https://avatars.githubusercontent.com/u/1225363?v=3","gravatar_id":"","url":"https://api.github.com/users/kneth","html_url":"https://github.com/kneth","followers_url":"https://api.github.com/users/kneth/followers","following_url":"https://api.github.com/users/kneth/following{/other_user}","gists_url":"https://api.github.com/users/kneth/gists{/gist_id}","starred_url":"https://api.github.com/users/kneth/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kneth/subscriptions","organizations_url":"https://api.github.com/users/kneth/orgs","repos_url":"https://api.github.com/users/kneth/repos","events_url":"https://api.github.com/users/kneth/events{/privacy}","received_events_url":"https://api.github.com/users/kneth/received_events","type":"User","site_admin":false},"body":"Migrating the old C macro based exception handling to modern C++ lambda functions. Only C++ exception is thrown in the C++ code, and they are converted to Java exception. The idea is that all JNI border functions must wrap all code in `try_catch()`. When writing C++ code (core, object store or JNI), only C++ exceptions should be thrown - no need to \"throw\" Java exception. The function `try_catch()` takes care of it.\r\n\r\nI do dislike big PRs, and this is definitely a big one. Let me give a short guide to the reviewers:\r\n\r\n* Focus on `util.hpp` and `util.cpp`\r\n* Take one JNI method to see how it works\r\n\r\n@realm/java ","created_at":"2016-10-10T15:26:32Z","updated_at":"2016-10-11T07:48:22Z","closed_at":null,"merged_at":null,"merge_commit_sha":null,"assignee":{"login":"kneth","id":1225363,"avatar_url":"https://avatars.githubusercontent.com/u/1225363?v=3","gravatar_id":"","url":"https://api.github.com/users/kneth","html_url":"https://github.com/kneth","followers_url":"https://api.github.com/users/kneth/followers","following_url":"https://api.github.com/users/kneth/following{/other_user}","gists_url":"https://api.github.com/users/kneth/gists{/gist_id}","starred_url":"https://api.github.com/users/kneth/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kneth/subscriptions","organizations_url":"https://api.github.com/users/kneth/orgs","repos_url":"https://api.github.com/users/kneth/repos","events_url":"https://api.github.com/users/kneth/events{/privacy}","received_events_url":"https://api.github.com/users/kneth/received_events","type":"User","site_admin":false},"assignees":[{"login":"kneth","id":1225363,"avatar_url":"https://avatars.githubusercontent.com/u/1225363?v=3","gravatar_id":"","url":"https://api.github.com/users/kneth","html_url":"https://github.com/kneth","followers_url":"https://api.github.com/users/kneth/followers","following_url":"https://api.github.com/users/kneth/following{/other_user}","gists_url":"https://api.github.com/users/kneth/gists{/gist_id}","starred_url":"https://api.github.com/users/kneth/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kneth/subscriptions","organizations_url":"https://api.github.com/users/kneth/orgs","repos_url":"https://api.github.com/users/kneth/repos","events_url":"https://api.github.com/users/kneth/events{/privacy}","received_events_url":"https://api.github.com/users/kneth/received_events","type":"User","site_admin":false}],"milestone":null,"commits_url":"https://api.github.com/repos/realm/realm-java/pulls/3602/commits","review_comments_url":"https://api.github.com/repos/realm/realm-java/pulls/3602/comments","review_comment_url":"https://api.github.com/repos/realm/realm-java/pulls/comments{/number}","comments_url":"https://api.github.com/repos/realm/realm-java/issues/3602/comments","statuses_url":"https://api.github.com/repos/realm/realm-java/statuses/39d846308b488078baf95787d009f74f0b90b81d","head":{"label":"realm:kneth/jni/throw-c++-exceptions","ref":"kneth/jni/throw-c++-exceptions","sha":"39d846308b488078baf95787d009f74f0b90b81d","user":{"login":"realm","id":7575099,"avatar_url":"https://avatars.githubusercontent.com/u/7575099?v=3","gravatar_id":"","url":"https://api.github.com/users/realm","html_url":"https://github.com/realm","followers_url":"https://api.github.com/users/realm/followers","following_url":"https://api.github.com/users/realm/following{/other_user}","gists_url":"https://api.github.com/users/realm/gists{/gist_id}","starred_url":"https://api.github.com/users/realm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/realm/subscriptions","organizations_url":"https://api.github.com/users/realm/orgs","repos_url":"https://api.github.com/users/realm/repos","events_url":"https://api.github.com/users/realm/events{/privacy}","received_events_url":"https://api.github.com/users/realm/received_events","type":"Organization","site_admin":false},"repo":{"id":4084908,"name":"realm-java","full_name":"realm/realm-java","owner":{"login":"realm","id":7575099,"avatar_url":"https://avatars.githubusercontent.com/u/7575099?v=3","gravatar_id":"","url":"https://api.github.com/users/realm","html_url":"https://github.com/realm","followers_url":"https://api.github.com/users/realm/followers","following_url":"https://api.github.com/users/realm/following{/other_user}","gists_url":"https://api.github.com/users/realm/gists{/gist_id}","starred_url":"https://api.github.com/users/realm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/realm/subscriptions","organizations_url":"https://api.github.com/users/realm/orgs","repos_url":"https://api.github.com/users/realm/repos","events_url":"https://api.github.com/users/realm/events{/privacy}","received_events_url":"https://api.github.com/users/realm/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/realm/realm-java","description":"Realm is a mobile database: a replacement for SQLite & ORMs","fork":false,"url":"https://api.github.com/repos/realm/realm-java","forks_url":"https://api.github.com/repos/realm/realm-java/forks","keys_url":"https://api.github.com/repos/realm/realm-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/realm/realm-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/realm/realm-java/teams","hooks_url":"https://api.github.com/repos/realm/realm-java/hooks","issue_events_url":"https://api.github.com/repos/realm/realm-java/issues/events{/number}","events_url":"https://api.github.com/repos/realm/realm-java/events","assignees_url":"https://api.github.com/repos/realm/realm-java/assignees{/user}","branches_url":"https://api.github.com/repos/realm/realm-java/branches{/branch}","tags_url":"https://api.github.com/repos/realm/realm-java/tags","blobs_url":"https://api.github.com/repos/realm/realm-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/realm/realm-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/realm/realm-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/realm/realm-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/realm/realm-java/statuses/{sha}","languages_url":"https://api.github.com/repos/realm/realm-java/languages","stargazers_url":"https://api.github.com/repos/realm/realm-java/stargazers","contributors_url":"https://api.github.com/repos/realm/realm-java/contributors","subscribers_url":"https://api.github.com/repos/realm/realm-java/subscribers","subscription_url":"https://api.github.com/repos/realm/realm-java/subscription","commits_url":"https://api.github.com/repos/realm/realm-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/realm/realm-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/realm/realm-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/realm/realm-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/realm/realm-java/contents/{+path}","compare_url":"https://api.github.com/repos/realm/realm-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/realm/realm-java/merges","archive_url":"https://api.github.com/repos/realm/realm-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/realm/realm-java/downloads","issues_url":"https://api.github.com/repos/realm/realm-java/issues{/number}","pulls_url":"https://api.github.com/repos/realm/realm-java/pulls{/number}","milestones_url":"https://api.github.com/repos/realm/realm-java/milestones{/number}","notifications_url":"https://api.github.com/repos/realm/realm-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/realm/realm-java/labels{/name}","releases_url":"https://api.github.com/repos/realm/realm-java/releases{/id}","deployments_url":"https://api.github.com/repos/realm/realm-java/deployments","created_at":"2012-04-20T09:18:42Z","updated_at":"2016-10-11T03:31:14Z","pushed_at":"2016-10-11T07:41:59Z","git_url":"git://github.com/realm/realm-java.git","ssh_url":"git@github.com:realm/realm-java.git","clone_url":"https://github.com/realm/realm-java.git","svn_url":"https://github.com/realm/realm-java","homepage":"http://realm.io","size":68465,"stargazers_count":6183,"watchers_count":6183,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":940,"mirror_url":null,"open_issues_count":334,"forks":940,"open_issues":334,"watchers":6183,"default_branch":"master"}},"base":{"label":"realm:master","ref":"master","sha":"8e08b3925cee34b4269b132c6f630e04a7491cd3","user":{"login":"realm","id":7575099,"avatar_url":"https://avatars.githubusercontent.com/u/7575099?v=3","gravatar_id":"","url":"https://api.github.com/users/realm","html_url":"https://github.com/realm","followers_url":"https://api.github.com/users/realm/followers","following_url":"https://api.github.com/users/realm/following{/other_user}","gists_url":"https://api.github.com/users/realm/gists{/gist_id}","starred_url":"https://api.github.com/users/realm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/realm/subscriptions","organizations_url":"https://api.github.com/users/realm/orgs","repos_url":"https://api.github.com/users/realm/repos","events_url":"https://api.github.com/users/realm/events{/privacy}","received_events_url":"https://api.github.com/users/realm/received_events","type":"Organization","site_admin":false},"repo":{"id":4084908,"name":"realm-java","full_name":"realm/realm-java","owner":{"login":"realm","id":7575099,"avatar_url":"https://avatars.githubusercontent.com/u/7575099?v=3","gravatar_id":"","url":"https://api.github.com/users/realm","html_url":"https://github.com/realm","followers_url":"https://api.github.com/users/realm/followers","following_url":"https://api.github.com/users/realm/following{/other_user}","gists_url":"https://api.github.com/users/realm/gists{/gist_id}","starred_url":"https://api.github.com/users/realm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/realm/subscriptions","organizations_url":"https://api.github.com/users/realm/orgs","repos_url":"https://api.github.com/users/realm/repos","events_url":"https://api.github.com/users/realm/events{/privacy}","received_events_url":"https://api.github.com/users/realm/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/realm/realm-java","description":"Realm is a mobile database: a replacement for SQLite & ORMs","fork":false,"url":"https://api.github.com/repos/realm/realm-java","forks_url":"https://api.github.com/repos/realm/realm-java/forks","keys_url":"https://api.github.com/repos/realm/realm-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/realm/realm-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/realm/realm-java/teams","hooks_url":"https://api.github.com/repos/realm/realm-java/hooks","issue_events_url":"https://api.github.com/repos/realm/realm-java/issues/events{/number}","events_url":"https://api.github.com/repos/realm/realm-java/events","assignees_url":"https://api.github.com/repos/realm/realm-java/assignees{/user}","branches_url":"https://api.github.com/repos/realm/realm-java/branches{/branch}","tags_url":"https://api.github.com/repos/realm/realm-java/tags","blobs_url":"https://api.github.com/repos/realm/realm-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/realm/realm-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/realm/realm-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/realm/realm-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/realm/realm-java/statuses/{sha}","languages_url":"https://api.github.com/repos/realm/realm-java/languages","stargazers_url":"https://api.github.com/repos/realm/realm-java/stargazers","contributors_url":"https://api.github.com/repos/realm/realm-java/contributors","subscribers_url":"https://api.github.com/repos/realm/realm-java/subscribers","subscription_url":"https://api.github.com/repos/realm/realm-java/subscription","commits_url":"https://api.github.com/repos/realm/realm-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/realm/realm-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/realm/realm-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/realm/realm-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/realm/realm-java/contents/{+path}","compare_url":"https://api.github.com/repos/realm/realm-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/realm/realm-java/merges","archive_url":"https://api.github.com/repos/realm/realm-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/realm/realm-java/downloads","issues_url":"https://api.github.com/repos/realm/realm-java/issues{/number}","pulls_url":"https://api.github.com/repos/realm/realm-java/pulls{/number}","milestones_url":"https://api.github.com/repos/realm/realm-java/milestones{/number}","notifications_url":"https://api.github.com/repos/realm/realm-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/realm/realm-java/labels{/name}","releases_url":"https://api.github.com/repos/realm/realm-java/releases{/id}","deployments_url":"https://api.github.com/repos/realm/realm-java/deployments","created_at":"2012-04-20T09:18:42Z","updated_at":"2016-10-11T03:31:14Z","pushed_at":"2016-10-11T07:41:59Z","git_url":"git://github.com/realm/realm-java.git","ssh_url":"git@github.com:realm/realm-java.git","clone_url":"https://github.com/realm/realm-java.git","svn_url":"https://github.com/realm/realm-java","homepage":"http://realm.io","size":68465,"stargazers_count":6183,"watchers_count":6183,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":940,"mirror_url":null,"open_issues_count":334,"forks":940,"open_issues":334,"watchers":6183,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/realm/realm-java/pulls/3602"},"html":{"href":"https://github.com/realm/realm-java/pull/3602"},"issue":{"href":"https://api.github.com/repos/realm/realm-java/issues/3602"},"comments":{"href":"https://api.github.com/repos/realm/realm-java/issues/3602/comments"},"review_comments":{"href":"https://api.github.com/repos/realm/realm-java/pulls/3602/comments"},"review_comment":{"href":"https://api.github.com/repos/realm/realm-java/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/realm/realm-java/pulls/3602/commits"},"statuses":{"href":"https://api.github.com/repos/realm/realm-java/statuses/39d846308b488078baf95787d009f74f0b90b81d"}}}}
{"actor":{"display_login":"beeender"}}