異常處理的方式二:throws + 異常類型

/*
* 異常處理的方式二:throws + 異常類型
*
* 1. "throws + 異常類型"寫在方法的聲明處。指明此方法執行時,可能會拋出的異常類型。
* 一旦當方法體執行時,出現異常,仍會在異常代碼處生成一個異常類的對象,此對象知足throws後異常
* 類型時,就會被拋出。異常代碼後續的代碼,就再也不執行!
*
* 2. 體會:try-catch-finally:真正的將異常給處理掉了。
* throws的方式只是將異常拋給了方法的調用者。 並無真正將異常處理掉。
*
* 3. 開發中如何選擇使用try-catch-finally 仍是使用throws?
* 3.1 若是父類中被重寫的方法沒有throws方式處理異常,則子類重寫的方法也不能使用throws,意味着若是
* 子類重寫的方法中有異常,必須使用try-catch-finally方式處理。
* 3.2 執行的方法a中,前後又調用了另外的幾個方法,這幾個方法是遞進關係執行的。咱們建議這幾個方法使用throws
* 的方式進行處理。而執行的方法a能夠考慮使用try-catch-finally方式進行處理。
*
*/java

package com.ch.java1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ExceptionTest2 {
    
    
    public static void main(String[] args){
        try{
            method2();
            
        }catch(IOException e){
            e.printStackTrace();
        }
        
//        method3();
        
    }
    
    
    public static void method3(){
        try {
            method2();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    
    public static void method2() throws IOException{
        method1();
    }
    
    
    public static void method1() throws FileNotFoundException,IOException{
        File file = new File("hello1.txt");
        FileInputStream fis = new FileInputStream(file);
        
        int data = fis.read();
        while(data != -1){
            System.out.print((char)data);
            data = fis.read();
        }
        
        fis.close();
        
        System.out.println("hahaha!");
    }
    
    
}

 

/*
* 方法重寫的規則之一:
* 子類重寫的方法拋出的異常類型不大於父類被重寫的方法拋出的異常類型ide

*/this

 

package com.ch.java1;

import java.io.FileNotFoundException;
import java.io.IOException;

/*
 * 方法重寫的規則之一:
 * 子類重寫的方法拋出的異常類型不大於父類被重寫的方法拋出的異常類型
 */
public class OverrideTest {
    
    public static void main(String[] args) {
        OverrideTest test = new OverrideTest();
        test.display(new SubClass());
    }

    
    public void display(SuperClass s){
        try {
            s.method();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class SuperClass{
    
    public void method() throws IOException{
        
    }
    
    
}

class SubClass extends SuperClass{
    public void method()throws FileNotFoundException{
        
    }
}

 手動拋出異常對象spa

package com.ch.java2;

public class StudentTest {
    
    public static void main(String[] args) {
        try {
            Student s = new Student();
            s.regist(-1001);
            System.out.println(s);
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }
    
}


class Student{
    
    private int id;
    
    public void regist(int id) throws Exception {
        if(id > 0){
            this.id = id;
        }else{
//            System.out.println("您輸入的數據非法!");
            //手動拋出異常對象
//            throw new RuntimeException("您輸入的數據非法!");
//            throw new Exception("您輸入的數據非法!");
            throw new MyException("不能輸入負數"); //錯誤的
//            throw new String("不能輸入負數");
        }
        
    }

    @Override
    public String toString() {
        return "Student [id=" + id + "]";
    }
    
    
}
相關文章
相關標籤/搜索