Solved

How to throw Exceptions in JNI

Posted on 2004-08-16
9
478 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
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!

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Eclipse Java import and method not resolved 4 141
dao vs facade design patterns 2 76
Java array 21 154
junit initializtion error 2 22
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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 …
Suggested Courses

710 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