200 likes | 355 Views
包、异常与输入输出 2. 本单元教学内容. 异常的基本概念和 Java 异常处理机制 掌握 try - catch 语句的用法 掌握 throws 关键字的用法. 什么是异常?. class MyMath{ public int devide(int x,int y){ int result=x/y; return result; } } class MyMathTest{ public static void main(String[] args){ MyMath mobj; mobj=new MyMath();
E N D
本单元教学内容 • 异常的基本概念和Java异常处理机制 • 掌握 try-catch 语句的用法 • 掌握throws关键字的用法
什么是异常? • class MyMath{ • public int devide(int x,int y){ • int result=x/y; • return result; • } • } • class MyMathTest{ • public static void main(String[] args){ • MyMath mobj; • mobj=new MyMath(); • int result; • result=mobj.devide(3,0); • System.out.println("the result is " + result); • } • } 该程序编译能通过吗?运行程序,有什么信息返回?
异常 • Exception in thread "main" java.lang.ArithmeticException: / by zero • 这说明,程序发生了算术异常(ArithmeticException),非正常的结束了。这种情况就是我们说的异常 • 异常定义了程序中遇到的非致命的错误,而不是编译时的语法错误。如除数为0,打开一个不存在的文件,操作数越界等等。
异常的基本概念 • 运行时发生的错误称为异常。 • 如果不对异常进行处理,那么一旦引发异常,程序将突然中止。 • 要么控制权返回给操作系统。 • 要么系统处于死机崩溃状态。 • 因此,安全健壮的程序设计语言应当引入有效的异常处理机制
对MyMathTest类进行如下修改 class MyMathTest{ • public static void main(String[] args){ • try{ • MyMath mobj; • mobj=new MyMath(); • int result; • result=mobj.devide(3,0); • System.out.println("the result is " + result); • }catch(Exception e){ • System.out.println(e.getMessage()); • } • System.out.println("program is running here."); • } • } 该条语句执行了吗?
说明 • 我们看到,当我们在程序中加了红色的代码后,在出现了异常后,程序没有异常中止,而是正常的继续运行。为什么会这样呢? • 我们用try…catch语句对程序中可能出现异常的语句进行了处理 • try{ statements • }catch(Exception e){ statements • } 可能出现异常的语句 发生异常后的处理语句
将该语句改为result=mobj.devide(3,1)执行过程又如何?将该语句改为result=mobj.devide(3,1)执行过程又如何? try…catch语句执行过程 • try{ • MyMath mobj; • mobj=new MyMath(); • int result; • result=mobj.devide(3,0); • System.out.println("the result is " + result); • }catch(Exception e){ • System.out.println(e.getMessage()); • } • System.out.println("program is running here."); 当try代码块中的语句发生了异常 这条语句还会执行吗? 程序就会跳转到相应的catch代码块中执行 执行完catch代码块中的程序后,系统会继续执行catch代码块后的其他代码
catch语句块 • 当try代码块中的程序发生了异常,系统将这个异常发生的代码行号,类别等信息封装到一个对象中,并将这个对象传递给catch代码块 • catch(Exception e){ System.out.println(e.getMessage()); • } • Exception就是try代码块传递给catch代码块的变量类型,e就是变量名。 • 问题一:e可以改为其他的名字吗?
编程实践 • class NoCatch{ • public static void main(String[] args){ • String str=args[0]; • int i=Integer.parseInt(str); • System.out.println("输入的数据为:" + i); • System.out.println("here is the end of the program"); • } • } • 该程序运行时,如果没有输入相应的命令行参数,会怎样? • 如何解决?
如果此时输入了命令行参数,但是输入的为字母a,程序能正常运行吗?如果此时输入了命令行参数,但是输入的为字母a,程序能正常运行吗? 程序修改如下 • class NoCatch{ • public static void main(String[] args){ • try{ • String str=args[0]; • int i=Integer.parseInt(str); • System.out.println("输入的数据为:" + i); • }catch(ArrayIndexOutOfBoundsException e){ • System.out.println("没有输入命令行参数"); • } • System.out.println("here is the end of the program"); • } • } 红色字体的代码为新增的代码
为什么? • 出的是什么异常? • 请注意,我们在catch中捕获的是哪种类型的异常?而我们现在出现的是哪种异常? • 要处理这种新出现的异常,我们应该怎么做呢? • 新增一个catch块来捕获这个异常 • catch(NumberFormatExceptione1){ • System.out.println("输入数据的格式不正确"); • } • 运行结果怎样?还会出这种异常吗?为什么?
小结 try…catch • 语句格式 try{ statements //可能产生异常的程序代码 }catch(ExceptionType1 e){//ExceptionType1类型异常的catch子句 statements //出现ExceptionType1类型异常时的处理程序 }catch(ExceptionType2 e){// ExceptionType1类型异常的catch子句 statements //出现ExceptionType1类型异常时的处理程序 }…… finally{ statements //异常处理程序结构的统一出口 }
提问 • 我们现在知道,在Java中采用try…catch语句处理异常。 • 假设,MyMath类是由甲写的,而MyMathTest类是由乙写的,那么,在乙使用MyMathTest类时,怎么能知道devide方法有可能出现异常呢?他又怎么会想到用try…catch语句去处理呢? • 在Java中,我们可以通过throws关键字来解决这个问题。
修改MyMath类 • class MyMath{ • public int devide(int x,int y) throws Exception{ • int result=x/y; • return result; • } • } • class MyMathTest{ • public static void main(String[] args){ • MyMath mobj; • mobj=new MyMath(); • int result; • result=mobj.devide(3,1); • System.out.println("the result is " + result); • } • } • 注意,此时程序应该不会发生异常,但程序能编译通过吗? 声明devide方法有可能发生Exception类型的异常,但他本身并未处理该异常
将MyMathTest类中添加try…catch语句 • public static void main(String[] args){ • try{ • MyMath mobj; • mobj=new MyMath(); • int result; • result=mobj.devide(3,0); • System.out.println("the result is " + result); • }catch(Exception e){ • System.out.println(e.getMessage()); • } • } 此时能够编译通过吗? 对MyMath抛出的异常进行了处理
throws关键字 • 如果在一个方法中的语句执行时可能生成某种异常,但又不能确定如何处理,则此方法应声明抛出异常(如throws Exception),表明该方法不对这些异常进行处理,而由该方法的调用者负责处理。 • 对于检查型异常,调用者要么用try…catch语句进行处理(如上面我们的做法);要么继续抛出该异常(如在上面的程序中,我们是在main方法中调用devide方法的,如果在main方法中不处理Exception类型异常,可以继续抛出该异常)。 public static void main(String[] args) throws Exception
问题 • 我们在前面的例子中,如果我们要从键盘输入数据,我们是如何写的? • public static void main(String[] args) throws IOException{ • BufferedReader rd; • rd=new BufferedReader(new InputStreamReader(System.in)); • String str1=rd.readLine(); • …… • } • 为什么要添加throws IOException? • 如果不添加throws IOException,编译能通过吗?还可以怎样修改?
作业 • 作业:习题8