Java中异常的处理

/**
 * 异常
 * 异常处理方式一:try-catch-finally
 * 异常处理方式二: throws + 异常类型
 *
 * 异常的处理,抓抛模型
 * 过程一,“抛”,程序正常执行的过程中,一旦出现了异常,就会在异常代码出生成一个异常类的对象,并将此对象抛出,一旦抛出对象以后,后面代码不再执行
 *      关于异常对象的产生:1.系统自动生成的异常对象  2.手动的生成一个异常对象,并抛出(throw)
 * 过程二,“抓”,可以理解为异常的处理方式,1.try-catch-finally     2.throws
 *
 *体会:try-catch-finally是真正的将异常处理掉了  throws的方式只是将异常抛给了方法的调用者,并没有真正的处理掉
 *
 *开发中如何选择使用try-catch-finally还是throws?
 * 1.如果父类中被重写的方法没有throws方式处理异常,则子类重写方法也不能使用throws。意味着如何子类重写方法中的异常
 * 必须使用try-catch-finally方式处理。
 * 2.执行的方法a中,先后调用了其他几个方法,这几个方法是递进关系执行的,建议这几个方法使用throws方式处理,
 * 而执行的方法a可以考虑使用try-catch-finally方式进行处理异常
 *
 */
public class exceptionTest {
    public static void main(String[] args) {

        //try-catch-finally真正的将异常处理掉了
        try {
            Student student =new Student();
            student.regist(-10);
            System.out.println("sss");//异常后的代码不会执行

        }catch (Exception e){
            //e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }
}
class Student{
    private int id;
    public void regist(int id) throws Exception{
                                //throws抓取异常,交给调用者去处理异常
        if(id>0){
            this.id =id;
        }else {

            //throw手动抛出异常
            throw new Exception("您输入的数据非法!");
        }
    }
}