捕获和抛出异常

异常处理五个关键字

try , catch , finally , throw , throws

try catch finally(快捷键:选中要要监控的代码语句 快捷键: ctrl + alt + t)
 package exception;
 ​
 public class Test {
     public static void main(String[] args) {
         int a = 1;
         int b = 0;
 ​
         try{//try 监控区域
             System.out.println(a/b);
        }catch (ArithmeticException e){//catch 捕获异常
             System.out.println("程序出现异常,b不能为0");
        }finally {// 处理善后工作
             System.out.println("finally");
        }
         
         //finally 可以不要finally, 用: 假设IO,资源需要关闭
         
    }
 }
 ---------------------------------------------------------------------------------------------------
 ​
 // 选中要要监控的代码语句 快捷键: ctrl + alt + t
 package exception;
 ​
 public class Test {
     public static void main(String[] args) {
         int a = 1;
         int b = 0;
         
         // 假设要捕获多个异常: 异常类型:从小到大
         
         try{//try 监控区域
             System.out.println(a/b);
        }catch (Error e){//catch(想要捕获的异常类型!) 捕获异常
             System.out.println("Error");
        }catch (Exception e){
             System.out.println("Exception");
        }catch (Throwable t){
             System.out.println("Throwable");
        }
         finally {// 处理善后工作
             System.out.println("finally");
        }
 ​
         //finally 可以不要finally, 用: 假设IO,资源需要关闭
 ​
    }
 }
 ​
throw throws
 package exception;
 ​
 public class Test {
     public static void main(String[] args) {
 ​
         new Test().test(1,0);
    }
     
     public void test(int a , int b){
         if(b==0){// throw throws
             throw new ArithmeticException();//主动抛出异常,一般在方法中使用 throw
        }
         System.out.println(a/b);
    }
 }
 ---------------------
     package exception;
 ​
 public class Test {
     public static void main(String[] args) {
 ​
         try {
             new Test().test(1,0); // try catch
        } catch (ArithmeticException e) {
             e.printStackTrace();
        }
    }
     
     // 假设这个方法中,处理不了这个异常。方法上抛出异常
     public void test(int a , int b) throws ArithmeticException{ // throws
         if(b==0){// throw throws
             throw new ArithmeticException();//主动抛出异常,一般在方法中使用
        }
         System.out.println(a/b);
    }
 }

学习内容源自视频:b站狂神说Java

最新文章

  1. ubuntu笔记
  2. Trace和Debug主要用法
  3. CentOS7 安装kubernetes
  4. maven 项目出现 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
  5. 用JAVA写一个冒泡排序
  6. laravel整理笔记(一)
  7. [Day24]IO(转换流、缓冲流)
  8. (译)MySQL 8.0实验室---MySQL中的倒序索引(Descending Indexes)
  9. BABOK概述
  10. use . adb . get wifi
  11. Android开发 - 获取系统输入法高度的正确姿势
  12. Ubuntu 14.04 vi 退格键不能删除字符
  13. 缺少dll文件的解决方法
  14. (回溯法)数组中和为S的N个数
  15. 【转】Linux(BASH)命令搜索机制
  16. Flink学习笔记:Connectors之kafka
  17. adminlte+layui框架搭建1
  18. hadoop-2.0.0-cdh4.6.0、sqoop-1.4.3-cdh4.6.0、mahout-0.7-cdh4.6.0 安装笔记
  19. Git使用之Permission Denied问题解决
  20. Java实现求二叉树的路径和

热门文章

  1. Go 中的 channel 与 Java BlockingQueue 的本质区别
  2. POJ 1556 计算几何 判断线段相交 最短路
  3. CRM的职能和主要构成模块探索
  4. 查看python的安装版本,位数及安装路径
  5. ES6 箭头函数你正确使用了吗
  6. 「CF986F」 Oppa Funcan Style Remastered
  7. c语言:DEV-C++5.10调试设置
  8. urllib库中的URL编码解码和GETPOST请求
  9. 开源桌面快速启动工具-GeekDesk
  10. js中 typeof 和 instanceof 的区别