Java exception throws && throw 的区别

来源:互联网 发布:淘宝男质量好的店铺 编辑:程序博客网 时间:2024/05/17 09:41

throws:用于在方法上声明该方法不需要处理的异常类型。
throw:用于抛出具体异常类的对象。

throws与throw的区别:
thorws用在方法上,后面跟异常类名,可以是多个异常类,留给调用处理异常,如果调用不处理,则JVM处理。
throw用在方法内,后面跟异常对象,只能是一个。抛出异常类的实例化处理。

public class ExceptionDemo4 {    public static void main(String[] args) throws NumberFormatException, FileNotFoundException  {        new FileInputStream("ooxx.txt");    }}
public class ExceptionDemo4 {    public static void main(String[] args)  {        try {            new FileInputStream("ooxx.txt");        } catch (FileNotFoundException e) {            e.printStackTrace();        }    }

related blog