Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7933625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:23:29+00:00 2026-06-03T21:23:29+00:00

The following is a java code and next to it is the c code

  • 0

The following is a java code and next to it is the c code :

package Package;

public class CatchThrow {

private native void doit() throws IllegalArgumentException;

private void callBack() throws NullPointerException {
    System.out.println("In call-back !");
    throw new NullPointerException("CatchThrow.callBack");
}

public static void main(String args[]) {
    CatchThrow c = new CatchThrow();
    try {
        c.doit();
    } catch(Exception exc) {
        System.out.println("\nIn Java : \n\t" + exc);
    }
}

 static {
     System.loadLibrary("CatchThrow");
 }
}

C code :

#include "stdio.h"
#include "Package_CatchThrow.h"

void Java_Package_CatchThrow_doit
   (JNIEnv *env, jobject obj) {
  jthrowable exc;
  jclass cls = (*env)->GetObjectClass(env,obj);
  jmethodID mid = (*env)->GetMethodID(env,cls,"callBack","()V");
  if(mid == NULL) {
    return;
  }
  (*env)->CallVoidMethod(env,obj,mid); // call the java method that throws NullPointerException
  printf("After the call to Call-Back !");
  exc = (*env)->ExceptionOccurred(env);
  if(exc) {
    jclass newExcCls;
    printf("\n");
    //(*env)->ExceptionDescribe(env);  ----> NOTE THE COMMENTED STATEMENT
    //(*env)->ExceptionClear(env);  -----> NOTE THE COMMENTED STATEMENT
    newExcCls = (*env)->FindClass(env,"java/lang/IllegalArgumentException");
    if(newExcCls == NULL) {
        return;
    }
    (*env)->ThrowNew(env,newExcCls,"thrown from c code");
  }

}

When i build and run the above program I get the following output :

In call-back !
After the call to Call-Back !

In Java :
    java.lang.IllegalArgumentException: thrown from c code

From the output : C code calls the java function callBack. There the first statement gets printed. After the call returns the statement next to the call i.e After the call to Call-Back ! gets printed. But what happened to the statement throw new NullPointerException("CatchThrow.callBack"); in the java function callBack ? Why doesn’t it print the exception ?

But if i remove the comments from the statements

(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);

commented, I get the desired output :

In call-back !
After the call to Call-Back !
Exception in thread "main" java.lang.NullPointerException: CatchThrow.callBack
    at Package.CatchThrow.callBack(CatchThrow.java:14)
    at Package.CatchThrow.doit(Native Method)
    at Package.CatchThrow.main(CatchThrow.java:20)

In Java :
    java.lang.IllegalArgumentException: thrown from c code

Why is that ?

What is the role of those two statements ? And why doesn’t the exception get printed in the absence of those 2 statements ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:23:30+00:00Added an answer on June 3, 2026 at 9:23 pm

    Your new NullPointerException("CatchThrow.callBack"); never touches the Java runtime and is handled completely in JNI space.

    As such, no exception details will be printed unless you call the JNI ExceptionDescribe function:

    (*env)->ExceptionDescribe(env); 
    

    You can’t return multiple exceptions from the JNI so the only exception information that comes back to your try/catch block in the Java runtime is the one you create later on with:

    (*env)->ThrowNew(env,newExcCls,"thrown from c code"); 
    

    A pending exception raised through the JNI (by calling ThrowNew, for
    example) does not immediately disrupt the native method execution.
    This is different from how exceptions behave in the Java programming
    language. When an exception is thrown in the Java programming
    language, the virtual machine automatically transfers the control flow
    to the nearest enclosing try/catch statement that matches the
    exception type. The virtual machine then clears the pending exception
    and executes the exception handler. In contrast, JNI programmers must
    explicitly implement the control flow after an exception has occurred.

    From the JNI documentation on exceptions

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following java code: public class CheckInnerStatic { private static class Test
The following java code prints Hello World on my co-worker's computer: public void testJDBC()
Say suppose I have the following Java code. public class Example { public static
I have the following code: String table; private DB.ConnectMAS mas=new DB.ConnectMAS(); java.sql.ResultSet rs1 =
Following is the errorneous code in Java. package wrapper; import java.util.ArrayList; import java.util.Iterator; import
So I have the following code: import java.lang.Thread; import java.lang.Integer; class MyThread extends Thread
Looking for help with the following code: package pkgPeople; import java.io.File; import java.io.FileOutputStream; import
The following Java code is throwing a compiler error: if ( checkGameTitle(currGame) ) ArrayList<String>
If I have the following Java code: int[][] readAPuzzle() { Scanner input = new
Consider the following Java code: volatile boolean v1 = false; volatile boolean v2 =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.