简体   繁体   中英

Why do i get this message when i run the java program?

Java Code :

package Package;

public class MyExceptionTester {

private native void compute() throws Exception;

public static void main(String... args) {
    try {
        MyExceptionTester met = new MyExceptionTester();
        met.compute(); // This method will throw an exception which gets printed from the catch block
    } catch(Exception exc) {
        System.out.println("From Java :" + exc);
    }
}

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

C++ Code :

#include "iostream"
#include "Package_MyExceptionTester.h"

void Java_Package_MyExceptionTester_compute
   (JNIEnv *env, jobject obj) {
        jthrowable exc;
        try {
            jint i = 1/0;
            throw "C++ Message : Hey ! Can't Divide By Zero";
        } catch(char *str) {
            jclass excClass = env->FindClass("java/lang/Exception");
            if(excClass == NULL) {
                    return;
                }
            env->ThrowNew(excClass,str);
          }

}

When i run the java program after including the dll i get the following message :

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_INT_DIVIDE_BY_ZERO (0xc0000094) at pc=0x65c4121a, pid=5292, tid=6000 
#
# JRE version: 7.0
# Java VM: Java HotSpot(TM) Client VM (20.0-b01 mixed mode, sharing windows-x86 )
# Problematic frame:
# C  [MyExceptionTester.dll+0x121a] 
#
# An error report file with more information is saved as:
# W:\elita\jnitesters\workspace\java\JNI\build\classes\hs_err_pid5292.log
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

Why do i get this message ? And why don't i see the output which should be a message printed from the java catch block .

 jint i = 1/0;
 throw "C++ Message : Hey ! Can't Divide By Zero";

This exception is not caught by your try/catch block, as the program crashes before the throw statement. And a division by 0 is not an exception you can catch.

Perhaps something that might help:

int d = 0;
jthrowable exc;
try 
{
    if ( d == 0 )
        throw "C++ Message : Hey ! Can't Divide By Zero";
    jint i = 1/d;
} catch(char *str) 
{
    jclass excClass = env->FindClass("java/lang/Exception");
    if(excClass == NULL) 
    {
       return;
    }
    env->ThrowNew(excClass,str);
}

Native code call has been inside JVM. Since the native program crashes before throwing exception, the crash in native programs causes JVM to crash too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM