Presentation is loading. Please wait.

Presentation is loading. Please wait.

第七章 异常.

Similar presentations


Presentation on theme: "第七章 异常."— Presentation transcript:

1 第七章 异常

2 知识点 掌握Java规范中非常重要的异常处理机制 会使用try和catch进来捕获和处理异常 掌握如何自定义异常类
掌握使用异常时一般遵守的原则 熟悉Java系统定义的标准异常

3 7.1异常的分类 Java把程序运行时的可能遇到的错误分为两类: 一类是非致命的,通过某种修正后程序还能继续执行。这类错误叫作异常。 如:文件不存在、无效的下标、空引用、网络断开、打印机脱机、磁盘满了等 另一类是致命的,即程序遇到了非常严重的不正常状态,不能简单恢复执行,这就是错误。如:内存耗尽、系统内部错误等

4 Public class NullExceptionDemo(){
Car myCar; void start(){ myCar.startEngine(); } Public static void main(stringE[] args){ NullExceptionDemo exceptionDemo=new NullExceptionDemo; exceptionDemo.start(); System.out.println(“start Car”); } }

5 所有的异常对象都继承自Throwable类的实例
Exception Error IOException Runtime Exception

6 Error分支用于Java 运行时系统的内部错误和资源耗尽
Exception是要重点掌握的,它也分为两类: Runtime Exception:是编程错误。造型转换、数组越界、访问空指针 非Runtime Exception :是运行时出现的意外。访问错误的URL、读取文件结尾后面的数据 异常处理的任务就是把控制从错误产生的地方转移到能够进行错误处理的地方。

7 当Java应用程序出现错误时,会产生一个异常对象,这个对象包含了异常的类型和错误出现时程序所处的状态信息。
7.2异常的抛出 当Java应用程序出现错误时,会产生一个异常对象,这个对象包含了异常的类型和错误出现时程序所处的状态信息。 把异常对象递交给Java编译器的过程称为抛出。 一个方法不仅能告诉Java编译器它能返回什么值,还可以告诉Java编译器有可能产生什么样的错.例: public String readline() throw IOException

8 如何抛出异常 精确定位错误 首先决定抛出异常类型.例如:当从一个长为1024的文件中读取数据时,但 读到688时遇到了文件结束标记,此时应抛出一个异常,EOFException比较合适 代码为:throw new EOFException(); 或: EOFException e= new EOFException(); throw e; 一个方法抛出了异常后,那么它再也不能返回调用者了 String readData(Bufferreader in) throws EOFException { … if(…..) throw new EOFException

9 class DutyException extends Exception
{int age,high,weight; DutyException() {super(); } DutyException(String message) {super(message);

10 class BodyException extends DutyException
{ BodyException() {super("体格例外"); } BodyException(String message) {super(message);

11 class HighException extends BodyException
{super("身高例外"); } HighException(int high,int highest,String message) {super(message); this.high=high;

12 class WeightException extends BodyException
{int weightest; WeightException() {super("体重例外"); } WeightException(int weight,int weightest,String message) {super(message); this.weight=weight; public boolean over() {if(weight>weightest)return(true); else return(false);}}

13 class AgeException extends DutyException
{ AgeException() {super(“年龄例外”); } AgeException(String message) {super(message); }} class ValueException extends AgeException { ValueException() {super(“年龄不合理例外”); } ValueException(String message) {super(message); } }

14 class SuitableException extends AgeException
{ SuitableException() {super("年龄不适合例外"); } SuitableException(int age,int limit,String message) {super(message); this.age=age;

15 catch指明要捕获的异常及相应处理代码
7.3异常的捕获和处理 异常发生时,Java运行系统从生成的代 码块开始,寻找相应的处理代码,并将 异常交给该方法处理,这一过程叫作捕获 若要捕获一个异常,需要设置一个 try/catch块: try语句括住可能抛出异常的代码段 catch指明要捕获的异常及相应处理代码

16 代码如下: try{…. } catch(Exception e) {….

17 例:一个捕获异常的例子 public void read(reader) { try {boolean done=false;
while(!done) {String lin=reader.readLine(); if(line==null) done=true; else {……} } } catch(IOException ) {…………}

18 }catch(ExceptionType1 e){ 抛出exceptionType1时要执行的代码
捕获多个异常 可以在一个try块中捕获多个异常类型,每个异常类型需要个单独的catch子句 try{ 抛出异常代码 }catch(ExceptionType1 e){ 抛出exceptionType1时要执行的代码 } catch(ExceptionType2 e){ 抛出exceptionType2时要执行的代码 } catch(ExceptionType3 e){ 抛出exceptionType3时要执行的代码…….. }

19 finally子句是一个不管是否出现异常,都必须被执行的程序段,所以常把一些必须被执行的代码放在finally子句里
1。代码不抛出任何异常:try--finally 2。代码抛出一个catch子句能够执行的异常 try--catch--finally 3。代码抛出一个catch子句不能够执行的异常 try--finally

20 public class ExceptionTest{
public static void high(int high,int weight,int age)throws AgeException,BodyException {if(age<0)throw new ValueException("没这种年龄"); else if(age<20||age>45)throw new SuitableException(); else if(200<high||high<150)throw new HighException(); else if (weight>100)throw new WeightException(weight,100,"太重了"); else if (weight<50)throw new WeightException(); }

21 public static void main(String args[])
{Integer index=null; int h,w,a; h=Integer.parseInt(args[0]); w=Integer.parseInt(args[1]); a=Integer.parseInt(args[2]);

22 try { high(h,w,a); } catch(WeightException e) { if(e.over())System.out.println("过重"); e.printStackTrace(); } catch(DutyException e) { System.out.println("in catch area"); System.out.println("getmessage"+e.getMessage()); e.printStackTrace(); } finally {System.out.println(“finally区块”); }}}

23 一般来说:确切知道如何处理的异常应该捕获,而把那些不知道如何去处理的异常只做抛出,将它传递给调用者去处理。
重置异常 catch(Exception e) { System.err.println ("An exception was thrown"); throw e; } 这个异常会被上层处理

24 7.4自定义异常类 用处:用于处理用户程序中特定的逻辑运行错误 方法:创建一个Exception和其它异常类的子类,
根据需要重载父类中的方法和属性,使自 定义异常类能够提供完整的错误信息 例:自定义异常类的使用

25 Import java.io.*; Class MyArithmeticException extends Exception { public MyArithmeticException{} public MyArithmeticException (String message) {super(message); }

26 Public class CustomizedExceptionDemo{
int div(int d1,int d2) trows MyArithmeticException { if (d2==0) throws new MyArithmeticException( “divide by zero”); return d1/d2; }

27 public static void main(string[] args){
CustomizedExceptionDemo demo= new CustomizedExceptionDemo try { demo.div(1,0); }catch(MyArithmeticException e) { System.out.println(“Caught exception:”+e.getMessage(); e.printStackTrace(); } } }

28 7.5常用异常类介绍 ClassNotFoundException 类或接口不存在 IOException 输入输出异常 FileNotFoundException 文件没找到 MalformedURLException URL 不正确 IndexOutofBoundException下标越界异常 ArrayIndexOutofBoundException ArithmeticException 除数为0时产生异常例:ExceptTest.java


Download ppt "第七章 异常."

Similar presentations


Ads by Google