java基础知识十二

第十二章

  • 异常

异常(Exception):又称为例外,是程序在运行过程中发生的非正常事件,其发生会影响程序的正常执行。Exception是程序级错误,可由程序本身处理;Error是系统级错误,程序可不用处理。
Java异常类都必须继承Throwable类或其子类。用户通过继承自定义异常。
常见异常:除数为零、负数开方、数组越界、I/O异常。

抛出的异常由catch捕获,未被捕获的异常逐层传播直到main。如果main也没有处理该异常,则操作系统会终止main执行。

处理异常时,也可以抛出新异常,也可以转发现有异常:链式异常。
抛出而未捕获(未处理)异常的函数,必须用throws声明其未处理的这些异常。

//Main.Java主程序
package  testException;
import java.lang.System;
import java.lang.ArithmeticException;
public  class  Main {
    static  int  div(int x, int y) throws  ArithmeticException{//处理了但又抛出了,故需声明
        try{ return x/y; }             //若y为0,系统自动产生除数为0异常
        catch(ArithmeticException ae){
            System.out.print(ae.toString( ));
            throw ae;                   //将逐层往上传递直到操作系统捕获为止   
        }
    }
    public  static  void  main(String[ ] args) {
        div(5, 0);                       //产生的异常main也没有处理,所以会导致main被中止执行
    }
}
  • 异常捕获顺序

无论何时,throw以后的语句都不会执行。
无论同层catch是否捕获、处理本层的异常(即使抛出或转发异常),同层的finally总是都会执行。
一个catch捕获到异常后,同层catch都不会执行,然后执行同层finally。
一个同层try-catch-finally结束,若无异常则执行其后语句,若有异常则跳过其后语句,进入上层catch流程。

//Main.Java主程序
package  testMoreException;
import java.lang.System;
import java.lang.ArithmeticException;
public  class  Main {
    static int div(int x, int y) {  //各种Exception都被捕获,函数无须声明异常
        int   r=0;
        try{  if(y==0) throw  new ArithmeticException( );      r=x/y; }   //通过new产生异常对象,程序自己抛出
        catch(ArithmeticException  ae){   System.out.print(ae.toString( ));   }   //捕获除数为0异常
        catch(Exception  ae){     System.out.print(ae.toString( ));   }    //捕获各种Exception:若第个1catch执行则无机会
        finally{  r=-1; }      //无论是否有异常,r=-1
        return  r;               //若有Error类型的异常,则不会执行该语句。ERROR不是Exception的子类
    }
    public  static  void  main(String[ ] args) {
        try{   div(5, 0);   } //调用div(5, 0)后,div函数的执行轨迹已用红色标出
        catch(Throwable  ae){     System.out.print(ae.toString( ));   }   //任何异常都被捕获,包括Error类型异常
    }
}
  • 自定义异常类

自定义异常类必须继承Throwable或其子类。
自定义异常类通常继承Exception及其子类,因为Exception是程序可处理的类。
自定义异常类会在父类的基础上增加成员变量,因此,通常需要覆盖toString函数(经常被打印)。未增加新成员就没有必要自定义异常类。

import  java.lang.Exception;
public class beyondMark extends Exception{
    int value, mark;
    public beyondMark(int v, int m){ value=v; mark=m; }
    public toString( ){ 
        return String.valueOf(value)+” beyonds “+String.valueOf(mark);
    }
}
//用例:catch(beyondMark b){ System.out.print(b.toString( )); }
  • 文本I/O
    • 类File: 路径管理,文件读写状态、修改获取日期等;并非读写文件。

file类的方法如下图:

【此处有图】

    • 类Scanner用于读取文件:File或InputStream的读入。可按串、字节、整数、双精度、或整行等不同要求读入。

【此处有图】

    • 类PrinterWriter用于输出到文件: File或InputStream的输出。 可按串、字节、整数、双精度、或整行等不同要求输出

【此处有图】

package  filecopy;
import java.lang.System;
import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.Scanner;
public class Main {     //一个简单的非二进制文件拷贝程序
    public static void main(String[ ] args) {  //参数不含程序名
        if(args.length!=2){
            System.out.println("Usage: Java  copy  <sourceFile>   <tagetFile>");
            System.exit(1);
        };
        File sF=new File(args[0]);                     //处理源文件
        if(!sF.exists( )){
            System.out.println(“Source File "+args[0]+ "does not exist!");
            System.exit(2);
        };
        File tF=new File(args[1]);      //处理目标文件
        if(tF.exists( )){
            System.out.println("Target File "+args[0]+ "already exist");
            System.exit(3);
        };
        try{
            Scanner input=new Scanner(sF);
            PrintWriter output=new PrintWriter(tF);
            while(input.hasNext( )){
                String s=input.nextLine( );  //每次读入一行
                output.println(s);
            }
            input.close( );       output.close( );
        }
        catch(IOException ioe){       System.out.println(ioe.toString( ));      }
    }
}

 在windows下,目录分隔符是反斜杠( \ ),但是在java中,反斜杠是一个特殊的字符,应写成\\的形式。

猜你喜欢

转载自www.cnblogs.com/jing-yu/p/9060717.html