Throwable 处理

异常:可以理解意料之外的情况,异常又分为很多类

  • 编译异常
  • 运行异常
  • 或断电、断网、服务宕机

异常类结构

Throwable 类是 Java 所有错误或者异常的超类,有 2 个子类

  • Throwable 异常
    • Error : 指合理的程序不应该试图捕获的严重问题,比如 VirtualMachineError Java 虚拟机错误。
    • Exception :指合理的应用程序想要捕获的条件,分为 2 类
      • RuntimeException : 由于程序错误导致的异常, 此类异常是编写者的问题
      • IOException : IO 异常
    • UncheckedException : 不需要捕获

代码中,异常注意事项

  • 子类的异常不能比父类大,如父类是 SQLException 异常,子类的异常就不能是 Exception
  • 子类的异常可以 = 父类的异常,就是说父类的异常和子类的异常在一个范围内
  • 使用 try … catch … 语句时,注意捕获异常的数据,小异常捕获在前,大异常捕获在后

异常

捕获异常

  1. public class ExceptionTest {
  2. public static void main(String[] args) {
  3. //发生异常之后的语句是不会执行的
  4. try {
  5. int x = 10;
  6. int y = 0;
  7. System.out.println(x/y);
  8. Class.forName("cn.com.A");
  9. }
  10. //捕获运算异常
  11. catch (ArithmeticException e) {
  12. e.printStackTrace();
  13. }
  14. //捕获找不到类异常
  15. catch (ClassNotFoundException e) {
  16. // 打印异常对象的堆栈信息
  17. e.printStackTrace();
  18. }
  19. //做一些释放资源的操作等等
  20. finally {
  21. System.out.println("异常也要执行!");
  22. }
  23. }
  24. }

自定义异常

  1. public class FileFormatException extends IOException{
  2. public FileFormatException() {
  3. }
  4. // 定义异常说明
  5. public FileFormatException(String gripe) {
  6. super(gripe);
  7. }
  8. //
  9. public String 抛出异常(String s) throws FileFormatException{
  10. if (s == null) {
  11. // 抛出异常
  12. throw new FileFormatException();
  13. }
  14. return s;
  15. }
  16. public void test (){
  17. try {
  18. this.抛出异常("13");
  19. }
  20. //捕获运算异常
  21. catch (FileFormatException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. }