Java异常学习

1.JDK中关于错误、异常的类级别

层级

  • Object
    • Throwable
      • Error
      • Exception

2.异常的分类

  • 编译时异常(需要显式地处理)
  • 运行时异常(RuntimeException,运行时会出现)

3.一些常见的异常

  • NullPointerException:空指针异常
  • ArithmeticException:算术异常
  • ArrayIndexOutOfBoundsException:数组索引越界异常
  • NumberFormatException:数据格式化异常
  • IOException:输入输出异常
  • FileNotFoundException:文件未找到异常
  • InterruptedException:中断异常
  • SQLException:数据库异常

4.如何处理异常

  • 抛出:在方法签名处用throws
  • 捕获:在调用处通过try-catch语句或者try-catch-finally语句实现

5.Java异常处理机制中涉及的几个关键字

  • throws
  • throw
  • try
  • catch
  • finally

6.异常处理的原则

  • 不要轻易地忽略应该捕获的Exception
  • 不要简单地捕获顶层的Exception

7.实际开发中

  • 良好的异常处理机制能让你的程序更“健壮”
  • 捕获异常并处理,给客户端适当的响应
  • 全局的异常处理设计

8.异常的一些示例程序

/**
 * 算术异常
 */
public class ArithmeticExceptionDemo {
    public static void main(String[] args) {
        int n = 3;
        try {
            System.out.println(n / 0);
        } catch (ArithmeticException e) {
            System.err.println("除数不能为0!");
        }
    }
}

/**
 * 数组索引越界异常
 */
public class ArrayIndexOutOfBoundsExceptionDemo {
    public static void main(String[] args) {
        int[] arr = new int[5];
        try {     //用try把有可能产生异常的代码块包起来
            for (int i = 0; i < 6; i++) {
                arr[i] = i + 1;
                System.out.println(arr[i]);
            }
        } catch (ArrayIndexOutOfBoundsException e) {  //catch捕获具体异常,并在方法体内友好处理
            System.err.println("数组下标越界");
        } finally {  //finally是不管有无异常都要执行的代码块
            System.out.println("数组输出完毕!");
        }
    }
}
/**
 * 数据格式转换异常
 */
public class NumberFormatExceptionDemo {
    public static void main(String[] args) {
        String s = "123a";
        int n = 0;
        try {
            n = Integer.parseInt(s);  //将"123a" 转换为整型,会触发数据格式转换异常
        } catch (NumberFormatException e) {
            System.err.println("格式转换出现异常");
        }
        System.out.println("n=" + n);
    }
}
/**
 * 中断异常
 */
public class InterruptedExceptionDemo {
    public static void main(String[] args) {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            System.err.println("出现中断异常!");
        }
    }
}
import java.io.*;

/**
 * IOException、FileNotFoundException练习
 */
public class IOExceptionDemo {
    public static void main(String[] args) {
        //文件指针指向d盘的1.jpg文件
        File file = new File("d:/1.jpg");
        //定义一个字节数组,长度和文件长度相等,用来读取文件
        byte[] b = new byte[(int) file.length()];
        InputStream inputStream = null;
        try {
            //通过file来创建字节输入流,如果文件不存在,会抛出FileNotFoundException
            inputStream = new FileInputStream(file);
            try {
                //通过字节输入流来读取文件内容到字节数组b中,会抛出IOException
                inputStream.read(b);
            } catch (IOException e) {
               System.err.println("字节流读取出现异常!");
            }
        } catch (FileNotFoundException e) {
            System.err.println("文件找不到!");
        }
        //关闭字节输入流,会抛IOException
        try {
            inputStream.close();
        } catch (IOException e) {
            System.err.println("字节流关闭出现异常!");
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 八、深入理解java异常处理机制 引子try…catch…finally恐怕是大家再熟悉不过的语句了, 你的答案是...
    壹点零阅读 1,642评论 0 0
  • Java异常类型 所有异常类型都是Throwable的子类,Throwable把异常分成两个不同分支的子类Erro...
    予别她阅读 961评论 0 2
  • 引言 在程序运行过程中(注意是运行阶段,程序可以通过编译),如果JVM检测出一个不可能执行的操作,就会出现运行时错...
    Steven1997阅读 2,506评论 1 6
  • 通俗编程——白话JAVA异常机制 - 代码之道,编程之法 - 博客频道 - CSDN.NEThttp://blog...
    葡萄喃喃呓语阅读 3,202评论 0 25
  • 与其在万人的天安门振臂高呼与其在千人的大会堂高屋建瓴与其在百人的阶梯室口若悬河与其在十人的会议桌运筹帷幄不如在一个...
    河岛阅读 287评论 7 8