异常处理的方式二: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方式进行处理。
*
*/

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!");
    }
    
    
}

/*
* 方法重写的规则之一:
* 子类重写的方法抛出的异常类型不大于父类被重写的方法抛出的异常类型

*/

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{
        
    }
}

 手动抛出异常对象

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 + "]";
    }
    
    
}
不积跬步,无以至千里;不积小流,无以成江海。
原文地址:https://www.cnblogs.com/CCTVCHCH/p/14615708.html