精华全集 冲广角币 获取广角币的更多方法
【名称】:操作异常
【作者】:江苏 无锡 缪小东
【格式】:PDF
【页数】:3
【语言】:中文
【出版社】:
【出版日期】:
【摘要或目录】:
本篇主要介绍如何在JNI处理java异常,这种异常可以java内部方法的,也可以是JNI中自己创建并且抛出的!同时还复习了JNI中操作java对象的方法! 一、Java类及两个辅助类 //下面是一个包含本地代码的java类!该本地方法调用java方法,同时可能抛出异常。 public class ExceptionAccess{ private static native void doThrowException(int age)throws AgeOutofBoundsException; //本地方法,调用Student类的setAge方法,抛出异常 public static void main(String[] args){ try { System.out.println("We are going to set the age ! age = 20"); ExceptionAccess.doThrowException(20); //正常处理 System.out.println(); System.out.println(); System.out.println("We are going to set the age ! age = 101"); //抛出异常 ExceptionAccess.doThrowException(101); }catch(AgeOutofBoundsException e ){ System.out.println("In Java:\n\t" + e); } } static { //不用说了吧! System.loadLibrary("ExceptionAccess"); } } //以下是一个简单的辅助类!包含一个抛出异常的方法! class Student{ private int age ; public void setAge(int age)throws AgeOutofBoundsException{ if(age > 100|| age<0) throw new AgeOutofBoundsException("\nAge is out of bound!Please check your age !\n"); this.age = age ; } public int getAge(){ return age ;