java第七章_异常处理(基础篇)_第1页
java第七章_异常处理(基础篇)_第2页
java第七章_异常处理(基础篇)_第3页
java第七章_异常处理(基础篇)_第4页
java第七章_异常处理(基础篇)_第5页
已阅读5页,还剩27页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第七章 异常处理1.1 错误处理的方法概述1.2 Java的异常处理机制1.3 创建自己的异常类1.1 错误处理的方法概述传统的程序运行时错误处理异常处理1 传统的程序运行时错误处理如 C语言: 函数返回值 =某个可能会执行失败的函数();if(函数返回值 =表示该函数执行成功的值) 正常代码 else if(函数返回值 =代表错误情况 1的值 )处理错误情形 1else if(函数返回值 =代表错误情况 2的值 )处理错误情形 21 传统的程序运行时错误处理 函数返回值 =某个可能会执行失败的函数(); if(函数返回值 !=表示该函数执行成功的值) Switch(函数返回值 )case 错误情况 1的值 :处理错误情形 1case 错误情况 2的值 :处理错误情形 2 else正常代码 1 传统的程序运行时错误处理缺点:1)整个程序代码穿插错误处理代码,使得条理性和可读性差;2)对错误处理程序难以集中管理,难以保证程序的一致性;3)对于返回值的意义,要借助于文档,程序维护困难。2 异常处理如 C+, JAVA语言: 就是在异常发生时,由编程语言提供的某种机制通知应用程序,让应用程序决定如何进行下一步的处理。传统方式: 1)负责测出错误的发生(程序设计者) 2)进行错误的处理异常处理方式: 2)进行错误的处理(程序设计者)2 异常处理 特点:1)可将错误处理代码和常规代码隔离开来,提高程序的可读性和可维护性;2)可以处理一组错误,进行集中管理,保证程序的一致性。3) 将异常事件分类,体现了良好的层次性。 1.2 Java 的异常处理机制Java语言用异常为它的程序提供了错误处理方式,为方法的异常终止和出错处理提供了清晰的接口。 异常和异常对象 异常的处理过程1 异常和异常对象 异常:在程序执行的时候,所发生会打断程序正常流程的事件。 异常的类型错误出现 产生异常对象 程序的状态信息“ 异常 ” 被看作是对象,是继承自 类Throwable的子类。所有 Throwable的子类所产生的对象实例,都是异常。1 异常和异常对象 异常类的层次 ErrorObjectThrowableExceptionRuntimeException Non_RuntimeException1 异常和异常对象 运行时 异常:运行异常可以不做处理,运行时系统会把生成的运行时异常对象交给默认的异常处理程序,在标准输出上显示异常的内容及发生异常的位置。1 异常和异常对象 ArithmeticException:除 0,用 0取模; NullPointException:访问一个空对象中的变量和方法,或一个空数组中的元素; ClassCastException:把对象 o强制成 Class C,而o既不是 C的 实例,又不是 C的子类的实例; NegativeArraySizeException:数组的长度是负数; ArrayIndexOutOfBoundsException:访问数组中的非法元素。1 异常和异常对象 非运行时 异常:非运行异常需要使用 try-catch-finally语句捕获异常或使用 throws子句生声明异常。1 异常和异常对象 IOException FileNotFoundException:找不到文件; InterruptedIOException; UnknownHostException; UnknownServiceException; SocketException; MalformedURLException; NoSuchMethodException ClassNotFoundException2 异常的处理过程 抛出异常 :产生一个异常对象以及把它转交给运行系统 间接抛出( try) 直接抛出( throw) 捕获异常 (catch): 找出异常的合适处理方法,即异常指针的选择过程 处理异常2 异常的处理过程Java的 异常处理机制由 try / catch / finally 组成try /可能会产生异常的程序块catch(Exception1)/异常处理 catch(Exception2)/异常处理finally/清除方法状态和关闭文件等语句2 异常的处理过程例 1:try System.in.read();catch(IOException e)String err = e.getMessage();System.out.println(err); / BubbleSort1.javapublic class BubbleSort1public static void main(String args) int a = new int10;for ( int i = 0; i 0; pass- ) for ( int i = 0; i b i + 2 ) swap(b,i,i + 1); public static void swap( int c, int first, int second )int hold; hold = c first ; c first = c second ; c second = hold; / BubbleSort2.javapublic class BubbleSort2public static void main(String args) int a = new int10;for ( int i = 0; i 0; pass- ) for ( int i = 0; i b i + 2 ) swap(b,i,i + 1); catch(ArrayIndexOutOfBoundsException e)System.out.println(“出现数组越界异常: “+e.getMessage(); public static void swap( int c, int first, int second )int hold; hold = c first ; c first = c second ; c second = hold; 2 异常的处理过程 -声明异常例 2:声明异常Protected void myMethod()try doRead();catch(IOException e)System.out.println(e.getMessage();Protected void doRead() throws IOExceptionSystem.in.read();2 异常的处理过程 抛出异常例 3:Public static void main(String args)try System.out.pringtln(passingGrade(90,80);catch(Exception e)System.out.println(e.getMessage(); Static void passingGrade(int correct,int total) throws Exception if(correcttotal)throw new Exception(“Invalid values”);2 异常的处理过程 Throw语句会使得一个异常被抛掷,其结果将导致程序控制的转换,即转向处理异常的catch块。形式:throw new Exception(“ 描述字符串 ” ); Throws是将本 方法产生的异常不处理,而是抛给上层调用者。1.3 创建自己的异常类 创建自己的异常类继承 Exception及其子类 ,多数情况下为非运行时异常。 如 class MyException extends Exception String say() 在程序中使用自己的异常类class UseMyException try throw new MyException();catch(MyException e)System.out.println(e.say(); 小结JAVA异常处理机制及处理过程异常处理与程序的结合使用异常,运行时异常,非运行时异常, Error,try-catch-finally,throw,throws习题解释 JAVA异常处理机制及处理过程解释异常,运行时异常,非运行时异常3. UsingExceptions.javapublic class UsingExceptions public static void main( String args ) try method1(); catch ( Exception e ) System.err.println( e.getMessage() + “n“ ); public static void method1() throws Exception method2(); public static void method2() throws Exception method3(); public static void method3() throws Exception throw new Exception( “Exception thrown in method3“ ); 4.UsingExceptions.javapublic class UsingExceptions public static void main( String args )try throwException(); catch ( Exception e ) System.err.println( “Exception handled in main“ ); public static void throwException() throws Exceptiontry System.out.println( “Method throwException“ );throw new Exception(); catch( RuntimeException e ) System.err.println( “Exception handled in “ +“method throwException“ ); finally System.err.println( “Finally is

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论