java异常

java异常的分类Throwable
1.IO异常2.运行时异常3.Errow
java异常的捕捉

  File file = new File("E:\\test\\test.exe");

这里new的只是一个pathname跟系统中是否存在该文件无关。路径也可以写成e:\test\test.exe。

 try{
           System.out.println("试图打开test.exe");
           new FileInputStream(file);//读取本地文件中的字节数据
       System.out.println("成功读取");

不存在的话会输出第一句,并在第二句事发现异常,随后进入捕捉异常区域

catch (FileNotFoundException e){//Exception e
          if(e instanceof FileNotFoundException)
          {System.out.println("文件不存在,请检查");
           e.printStackTrace();//打印栈调用踪迹(分析用)
}

finally是一定会执行的区域

finally {
           System.out.println("我是无论如何都会执行的");
       }

throws 抛出异常对象,由调用该方法的方法去catch,主函数要是没catch住系统报错。eg:

public class TextException {
    public static void main(String[] args) {
        methed1();
    }
    public static void methed1() {
        try { method2(); } 
        catch (FileNotFoundException e) { e.printStackTrace(); }
    }
    public  static  void method2() throws FileNotFoundException {
            File file = new File("e:/test.test.exe");
            FileInputStream fileInputStream = new FileInputStream(file);
            System.out.println("成功打开"); }
    }

该异常在method1中被catch不会再向上抛出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值