{"action":"created","issue":{"url":"https://api.github.com/repos/s-u/rJava/issues/60","labels_url":"https://api.github.com/repos/s-u/rJava/issues/60/labels{/name}","comments_url":"https://api.github.com/repos/s-u/rJava/issues/60/comments","events_url":"https://api.github.com/repos/s-u/rJava/issues/60/events","html_url":"https://github.com/s-u/rJava/issues/60","id":119471891,"number":60,"title":"Exception handling does not work when R.oo package is loaded","user":{"login":"CharlesCara","id":1662409,"avatar_url":"https://avatars.githubusercontent.com/u/1662409?v=3","gravatar_id":"","url":"https://api.github.com/users/CharlesCara","html_url":"https://github.com/CharlesCara","followers_url":"https://api.github.com/users/CharlesCara/followers","following_url":"https://api.github.com/users/CharlesCara/following{/other_user}","gists_url":"https://api.github.com/users/CharlesCara/gists{/gist_id}","starred_url":"https://api.github.com/users/CharlesCara/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CharlesCara/subscriptions","organizations_url":"https://api.github.com/users/CharlesCara/orgs","repos_url":"https://api.github.com/users/CharlesCara/repos","events_url":"https://api.github.com/users/CharlesCara/events{/privacy}","received_events_url":"https://api.github.com/users/CharlesCara/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-11-30T11:17:29Z","updated_at":"2016-01-30T14:10:06Z","closed_at":null,"body":"Hi \r\nI have found that some (but not all) packages disrupt the exception handling in rJava, preventing normal exceptions being Thrown and caught.  Instead a R error is thrown and the java exception details are lost.\r\n\r\nThe R error is: \r\n\r\n>  Error in ls(envir = envir, all.names = private) : \r\n>    invalid 'envir' argument\r\n\r\nThe problem exists on both my Windows and Ubuntu machines.  My minimal example is below:\r\n\r\n>require(rJava)\r\n>.jinit(force.init = TRUE)\r\n>Integer <- J(\"java.lang.Integer\")\r\n>tryCatch( Integer$parseInt( \"10..\" ), \r\n>          NumberFormatException = function(e){  e$jobj$printStackTrace() } )\r\n>require(mailR)\r\n>Integer <- J(\"java.lang.Integer\")\r\n>tryCatch( Integer$parseInt( \"10..\" ), \r\n>          NumberFormatException = function(e){  e$jobj$printStackTrace() } ) \r\n\r\n\r\nWhen run this gives:\r\n\r\n> require(rJava)\r\nWarning message:\r\npackage ‘rJava’ was built under R version 3.2.2 \r\n> .jinit(force.init = TRUE)\r\n> Integer <- J(\"java.lang.Integer\")\r\n> tryCatch( Integer$parseInt( \"10..\" ), \r\n>           NumberFormatException = function(e){  e$jobj$printStackTrace() } )\r\n>java.lang.NumberFormatException: For input string: \"10..\"\r\n>\tat java.lang.NumberFormatException.forInputString(Unknown Source)\r\n>\tat java.lang.Integer.parseInt(Unknown Source)\r\n>\tat java.lang.Integer.parseInt(Unknown Source)\r\n>\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n>\tat sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)\r\n>\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)\r\n>\tat java.lang.reflect.Method.invoke(Unknown Source)\r\n>\tat RJavaTools.invokeMethod(RJavaTools.java:386)\r\n> \r\n> require(mailR)\r\n>Loading required package: mailR\r\n> \r\n> \r\n> Integer <- J(\"java.lang.Integer\")\r\n> tryCatch( Integer$parseInt( \"10..\" ), \r\n>           NumberFormatException = function(e){  e$jobj$printStackTrace() } )\r\n>Error in ls(envir = envir, all.names = private) : \r\n>  invalid 'envir' argument\r\n\r\nSo when mailR is loaded your exception handling example does not give the exception in Java.  This is the stack trace:\r\n> traceback()\r\n>21: ls(envir = envir, all.names = private)\r\n>20: getFields.Object(x, ...)\r\n>19: getFields(x, ...)\r\n>18: names.Object(x)\r\n>17: names(x)\r\n>16: match(x, table, nomatch = 0L)\r\n>15: name %in% names(x)\r\n>14: `$.Throwable`(c, \"message\")\r\n>13: c$message\r\n>12: conditionMessage.condition(cond)\r\n>11: conditionMessage(cond)\r\n>10: stop(list(message = \"java.lang.NumberFormatException: For input string: \\\"10..\\\"\", \r\n>        call = .jcall(\"RJavaTools\", \"Ljava/lang/Object;\", \"invokeMethod\", \r\n>            cl, .jcast(if (inherits(o, \"jobjRef\") || inherits(o, \r\n>                \"jarrayRef\")) o else cl, \"java/lang/Object\"), .jnew(\"java/lang/String\", \r\n>                method), j_p, j_pc, use.true.class = TRUE, evalString = simplify, \r\n>            evalArray = FALSE), jobj = <S4 object of class \"jobjRef\">))\r\n>9: .Call(RJavaCheckExceptions, silent)\r\n>8: .jcheck(silent = FALSE)\r\n>7: .jcall(\"RJavaTools\", \"Ljava/lang/Object;\", \"invokeMethod\", cl, \r\n>       .jcast(if (inherits(o, \"jobjRef\") || inherits(o, \"jarrayRef\")) o else cl, \r\n>           \"java/lang/Object\"), .jnew(\"java/lang/String\", method), \r\n>       j_p, j_pc, use.true.class = TRUE, evalString = simplify, \r\n>       evalArray = FALSE)\r\n>6: .jrcall(x@name, name, ...)\r\n>5: Integer$parseInt(\"10..\")\r\n>4: doTryCatch(return(expr), name, parentenv, handler)\r\n>3: tryCatchOne(expr, names, parentenv, handlers[[1L]])\r\n>2: tryCatchList(expr, classes, parentenv, handlers)\r\n>1: tryCatch(Integer$parseInt(\"10..\"), NumberFormatException = function(e) {\r\n>       e$jobj$printStackTrace()\r\n>   })\r\n> \r\n\r\nBy trial and error, I have found that some packages (eg XLConnect) do not have this behavour.  I have tried modifying the .onLoad function of mailR to be the similar to XLConnect, but still this behaviour exists.\r\n\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/s-u/rJava/issues/comments/177188840","html_url":"https://github.com/s-u/rJava/issues/60#issuecomment-177188840","issue_url":"https://api.github.com/repos/s-u/rJava/issues/60","id":177188840,"user":{"login":"s-u","id":2073427,"avatar_url":"https://avatars.githubusercontent.com/u/2073427?v=3","gravatar_id":"","url":"https://api.github.com/users/s-u","html_url":"https://github.com/s-u","followers_url":"https://api.github.com/users/s-u/followers","following_url":"https://api.github.com/users/s-u/following{/other_user}","gists_url":"https://api.github.com/users/s-u/gists{/gist_id}","starred_url":"https://api.github.com/users/s-u/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/s-u/subscriptions","organizations_url":"https://api.github.com/users/s-u/orgs","repos_url":"https://api.github.com/users/s-u/repos","events_url":"https://api.github.com/users/s-u/events{/privacy}","received_events_url":"https://api.github.com/users/s-u/received_events","type":"User","site_admin":false},"created_at":"2016-01-30T14:10:06Z","updated_at":"2016-01-30T14:10:06Z","body":"Awesome, I forgot about `NextMethod()` - that's perfect :)"}}