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 8981637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:23:09+00:00 2026-06-15T20:23:09+00:00

I have the code on JNI level. And it can throws exceptions. The code

  • 0

I have the code on JNI level. And it can throws exceptions. The code is:

#include "JavaGlueClass.h"
#include <stdio.h>         
#include <windows.h>
#include <string.h>

jint throwNoClassDefError(JNIEnv *env, const char *message)
{
    jclass exClass;
    char *className = (char *) "java/lang/NoClassDefFoundError" ;

    exClass = env->FindClass(className);

    if ( exClass != NULL ) {
        return env->ThrowNew(exClass, message);
    }

    //free the local ref 
    env->DeleteLocalRef(exClass);

}

void throwGetFieldIDException(JNIEnv *env)
{
    const char *className = "GetFieldIDException";
    jclass exClass = env->FindClass(className);
    if (exClass == NULL) {
        throwNoClassDefError(env, className);
    } else {
        env->ThrowNew(exClass, "GetFieldIDException message");
    }
    env->DeleteLocalRef(exClass);
    printf("printprint");
}




JNIEXPORT jobject JNICALL Java_JavaGlueClass_test(JNIEnv *env, jobject obj) 
{
    jmethodID constructor;
    jobject object;
    jclass clazz;
    jfieldID fid;
    jstring stringField;

    clazz = env->FindClass("Information"); 
    if (clazz == 0) {
        printf("error while finding class");
        throwNoClassDefError(env, "no such class");
    } else {                
        //create object throuht constructor
        constructor = env->GetMethodID(clazz, "<init>", "()V");  
        object = env->NewObject(clazz, constructor);    

        // set private value1 field 
        stringField = env->NewStringUTF("str1");
        //fid = env->GetFieldID(clazz,"value1","Ljava/lang/String;");
        fid = NULL;
        if (fid == NULL) {
            throwGetFieldIDException(env, "error with value1 field.");
            //return NULL;
        }

        env->SetObjectField(object, fid, stringField);

        //set private value2 field
        fid = env->GetFieldID(clazz,"value2","I");
        if (fid == NULL) {
            throwGetFieldIDException(env, "error with value1 field.");
            //return NULL;
        }
        env->SetIntField(object, fid, 1); 

        // set private value3 field 
        stringField = env->NewStringUTF("str2");
        fid = env->GetFieldID(clazz,"value3","Ljava/lang/String;");
        if (fid == NULL) {
            throwGetFieldIDException(env, "error with value1 field.");
            //return NULL;
        }
        env->SetObjectField(object, fid, stringField);

        //set private value4 field
        fid = env->GetFieldID(clazz,"value4","I");
        if (fid == NULL) {
            throwGetFieldIDException(env, "error with value1 field.");
            //return NULL;
        }
        env->SetIntField(object, fid, 2); 

        printf("end of cpp function");              
        return object;
    }

}

Java_JavaGlueClass_test function creates an object of the custom Information class. Each time I set the class field, there is a check if “fid” is no NULL.
I read about JNI exceptions that those are pending exceptions. I figured out that first exception that will be thrown move the code flow to Java level (not JNI level). But what about the rest code after exception in the JNI level. As I understand it will be executed.
Is it possible that after first exception the second, third, etc. exceptions will be thrown? Should I return NULL or something after throwing exception in order the rest code in JNI level will not be executed?

  • 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-15T20:23:11+00:00Added an answer on June 15, 2026 at 8:23 pm

    From the JNI specification:

    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.

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

Sidebar

Related Questions

I have code that generates a List<string[]> variable but can't quite figure out how
I am writing a JNI application. In my C++ code, I have std::string .
I have an Java applet that loads native code through JNI. Everything worked just
I have a Java application which interacts with native code using JNI. The native
I have code in a managed bean: public void setTestProp(String newProp) { FacesMessage yourFailure
Can anyone guide on how to debug a JNI code on Linux using GDB
I have some JNI code in a thread that calls from Java to C.
I'd like a consistent and simple way to throw exceptions in JNI code; something
I have a JNI code that is used to make calls to .Net APIs
I have a C++ library and I made some JNI code to export it

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.