Solved

How to throw Exceptions in JNI

Posted on 2004-08-16
9
475 Views
Last Modified: 2013-11-23
Hello all,

How to throw Exceptions in JNI??

class  TestException
{
      public native String getFileType(String magicFile,String strFile)throws Exception;
      public static void main(String[] args)
      {
            System.out.println("Hello World!");
      }
}


javah generated code

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class TestException */

#ifndef _Included_TestException
#define _Included_TestException
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     TestException
 * Method:    getFileType
 * Signature: (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_TestException_getFileType
  (JNIEnv *, jobject, jstring, jstring);

#ifdef __cplusplus
}
#endif
#endif


JNIEXPORT jstring JNICALL Java_TestException_getFileType
  (JNIEnv *env, jobject object , jstring str1, jstring str2)
{
//some blah blah blah
 
 //now assume exception raised here. then how should c source should return exception object to JVM.
}

Thanks and Best Regards
Sudhakar
0
Comment
Question by:sudhakar_koundinya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 125 total points
ID: 11807568
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11807587
thanks. take ur A
0
 
LVL 6

Expert Comment

by:expertmb
ID: 11807591
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:expertmb
ID: 11807597
oooooooooopss!!!
forgot to reload the question
0
 
LVL 35

Expert Comment

by:girionis
ID: 11807602
Thank you for accepting, glad I was of help :)
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11807962
girionis

one quick question

after throwing error if i don't return anything JVM is raising exception. Is it must to return some thing
if(strlen(getError())>0)
      {
            jclass excCls = (*env)->FindClass(env,"java/lang/Exception");
            if (excCls != 0)
            {
                  (*env)->ThrowNew(env, excCls, getError());
                  return ""; //ok ->Is this must here??
                  //return ""; //not ok
            }
      }

0
 
LVL 35

Expert Comment

by:girionis
ID: 11808008
Are you calling it from a C function that returns something? If yes then I'd expect that you need to return something anyway.
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11808286
ok, I understand that

Thanks for nice links :-)
0
 
LVL 35

Expert Comment

by:girionis
ID: 11808308
:)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
add projects t working set in maven 2 37
junit as external jar or library 7 37
passing enum to a method 4 34
Coding for the first time 9 62
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question