Linux安全网 - Linux操作系统_Linux 命令_Linux教程_Linux黑客

会员投稿 投稿指南 本期推荐:
搜索:
您的位置: Linux安全网 > Linux编程 > » 正文

Step By Step(Java 本地方法篇)(12)

来源: 未知 分享至:
4 for (int i = 0; i < dd.length; ++i) {
5 dd[i] = i;
6 }
7 System.out.println(\"The Array before updated is \");
8 for (double d : dd)
9 System.out.print(d + \" \");
10 System.out.println();
11 UpdateArray.update(dd);
12 System.out.println(\"The Array after updated is \" + dd);
13 for (double d : dd)
14 System.out.print(d + \" \");
15 }
16 }
17 /* 输出结果如下:
18 The Array before updated is
19 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
20 The Array after updated is [D@13f3045
21 0.0 2.0 4.0 6.0 8.0 10.0 12.0 14.0 16.0 18.0
22 */

    12.    异常错误处理:
    在Java编程语言中,可以通过异常中断当前正在执行的操作,并退出当前操作所在的函数。然而当我们使用native方法的时候,由于是C语言并不支持异常,这样便需要JNI提供一组操作Java异常的方法,以便在二者之间可以共享异常信息。需要注意的是,即便C++中支持异常,也无法将C++中的异常和Java中的直接关联在一起,更无法在两者之间相互转换。这里我们还是通过代码示例的方式,并结合关键的注释来了解这些JNI函数的用法和机制。
    1)    包含native方法的Java类代码:

1     import java.io.PrintWriter;
2 public class Printf {
3 public static native void fprint(PrintWriter ps,String format,double x);
4 static {
5 System.loadLibrary(\"Printf\");
6 }
7 }

    2)    实现本地方法的C语言代码:

 1     #include \"Printf.h\"
2 #include <stdlib.h>
3 #include <string.h>
4 #include <float.h>
5 JNIEXPORT void JNICALL Java_Printf_fprint
6 (JNIEnv* env, jclass cl, jobject out, jstring format, jdouble x)
7 {
8 //这里是通过ThrowNew方法生成一个异常对象,这里FindClass查找的是
9 //运行时异常NullPointerException,需要注意的是,对于C语言的代码,
10 //在调用ThrowNew之后,并不会在C代码内部抛出异常,因此在调用ThrowNew后,
11 //其后面的代码仍然可以继续执行。然而由于异常标识一种错误的状态,因此
12 //在通常情况下,我们都会在ThrowNew的后面释放一些资源,之后立刻调用
13 //return退出当前的native方法。
14 //在native方法退出后,其调用的ThrowNew将会发挥作用,JVM会将抛出的
15 //异常返给native方法的最近调用者(Java代码)。
16 if (format == NULL) {
17 env->ThrowNew(env

Tags:
分享至:
最新图文资讯
1 2 3 4 5 6
验证码:点击我更换图片 理智评论文明上网,拒绝恶意谩骂 用户名:
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 发展历史