title: JNI Getting Exception Info - Attempted to read or write protected memory
body: <p>I'm using the following code to try and obtain a class RWException (which extends Exception) in java so that I can call a method "getCode()" to retrieve an error code (int) and handle the error properly.  I went through the JNI docs and created the following code...  problem is I am getting an AccessViolation exception when I attempt to call the parameterless method getCode().  I get a valid handle for the class and the method id I am looking for. </p>
  
  <pre><code>jstring o = (jstring)envLoc-&gt;CallStaticObjectMethod(cls, mid, jstrUser, jstrPass, jstrGroup);
  jthrowable exc = envLoc-&gt;ExceptionOccurred();
  
  if (exc) {
      // Get the class
      jclass mvclass = env-&gt;GetObjectClass( exc );
      // Get method ID for method
      jmethodID mid = env-&gt;GetMethodID(mvclass, "getCode", "()I");
      // Call the method      
      jint code  =  env-&gt;CallIntMethod(mvclass, mid);
  }
  </code></pre>
  
  <p>This code gives me an exception while debugging inVS.NET w/ the following info: </p>
  
  <p>Attempted to read or write protected memory</p>
  
  <p><strong>UPDATE</strong>
  Here is the java method I wish to invoke via JNI code above:</p>
  
  <pre><code>public int getCode() {
      return code;
  }
  </code></pre>
  
  <p>Both the mvclass and mid objects are instantiated properly and should function unless I am missing something.</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>If I run the following code the toString() method works using the same concept:</p>
  
  <pre><code>jstring o = (jstring)envLoc-&gt;CallStaticObjectMethod(cls, mid, jstrUser, jstrPass, jstrGroup);
  exc = envLoc-&gt;ExceptionOccurred();
  if (exc) {
  
      envLoc-&gt;ExceptionClear();
  
      // Get the class
      jclass exccls = envLoc-&gt;GetObjectClass(exc);
  
      // Get method ID for methods 
      jmethodID getCodeMeth = envLoc-&gt;GetMethodID(exccls, "getCode", "()I");
  
      jmethodID getMsgMeth = envLoc-&gt;GetMethodID(exccls, "toString", "()Ljava/lang/String;");
  
      jstring obj = (jstring)envLoc-&gt;CallObjectMethod(exccls, getMsgMeth);
      String^ toString = JStringToCliString(obj);
  
      // this is where the access violation occurs
      jint jcode  =  envLoc-&gt;CallIntMethod(exccls, getCodeMeth);
      int code = jcode;
  }
  </code></pre>
  
  <p>So, the toString() method returns the full class name of the object and it's the correct RWException object.  The method outlined in the first update getCode() is public, etc... so not sure why it gives a memory access violation error.</p>