• Scala 高阶(十):Scala中的异常处理


    大家好,我是百思不得小赵。

    创作时间:2022 年 7 月 27 日
    博客主页: 🔍点此进入博客主页
    —— 新时代的农民工 🙊
    —— 换一种思维逻辑去看待这个世界 👀
    今天是加入CSDN的第1243天。觉得有帮助麻烦👏点赞、🍀评论、❤️收藏



    Scala中的异常机制语法处理上和 Java 类似,但是又不尽相同。

    一、异常概述

    • 异常机制:程序在执行过程中发生了不正常的情况。

    代码演示:

    public class ExceptionText {
        public static void main(String[] args) {
            int a=100;
            int b=0;
            System.out.println(a/b);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    运行结果:

    二、回顾Java中的异常机制

    Java中异常处理有两种方式

    • 在方法声明的位置上,使用throws关键字,抛给上一级。谁调用我,我就抛给谁。抛给上一级。
    public class ExceptionText {
        public static void main(String[] args) throws Exception{
            int a=100;
            int b=0;
            divide(a,b);
        }
    
        public static void divide(int a, int b) throws Exception{
            int c = a / b;
            System.out.println(c);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 使用try…catch语句进行异常的捕捉。
    public class ExceptionText {
        public static void main(String[] args){
            int a=100;
            int b=0;
            try {
                divide(a,b);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        public static void divide(int a, int b) throws Exception{
            int c = a / b;
            System.out.println(c);
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    注意:

    1.Java中异常发生之后如果一直上抛,最终抛给了main方法,main方法继续向上抛,抛给了调用者JVM,JVM知道这个异常发生,只有一个结果。终止java程序的执行。
    2.try语句中某一行出现异常该行后续代码不执行try…catch捕获后,后续代码可执行。

    try…catch和finally概述

    • 语法格式:
    try {
    
    }catch(OneException e) { 
    
    }catch(TwoException e) {
    
    }finally {
     
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • try 中包含了可能产生异常的代码

    • try 后面是 catch,catch 可以有一个或多个,catch 中是需要捕获的异常

    • finally 表示:不管是出现异常,还是没有出现异常,finally 里的代码都执行,finally 和 catch
      可以分开使用,但 finally 必须和 try 一块使用

    try {
    
    }finally {
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • catch(异常1 | 异常2 |异常3 |......) jdk8新特性!!

    示例代码(1):

    public class ExceptionText {
        public static void main(String[] args){
            int a=100;
            int b=0;
            try {
                divide(a,b);
                //上一行代码有异常,直接进入catch里面!!
                System.out.println("我能执行吗?");
            } catch (Exception e) {
                System.out.println("被0除了!!");
            }
        }
        public static void divide(int a, int b) throws Exception{
            int c = a / b;
            System.out.println(c);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    运行结果:

    0除了!!
    
    • 1

    示例代码(2):

    public class ExceptionText {
        public static void main(String[] args){
            int a=100;
            int b=0;
            try {
                divide(a,b);
                //上一行代码有异常,直接进入catch里面!!
                System.out.println("我能执行吗?");
            } catch (Exception e) {
                System.out.println("被0除了!!");
            }finally {
                System.out.println("finally执行了!!");
            }
        }
        public static void divide(int a, int b) throws Exception{
            int c = a / b;
            System.out.println(c);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    运行结果:

    0除了!!
    finally执行了!!
    
    • 1
    • 2

    throws与try…catch如何选择?
    需要上报异常使用throws,需要捕获异常时使用try…catch进行捕获!!

    finally重要面试题

    示例代码(3):

    public class FinallyText {
        public static void main(String[] args) {
            System.out.println(n());
        }
        
        public static int n(){
            int i=100;
            try {
                return i;
            }finally {
                i++;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    三、Scala中的异常机制

    • 将会发生异常的代码封装在 try 块中。在 try 块之后使用了一个 catch 处理程序来捕获异常。如果发生任何异常,catch处理程序将处理它,程序将不会异常终止。
    • Scala 的异常的工作机制和 Java 一样,但是 Scala 没有“checked(编译期)”异常,即 Scala没有编译异常这个概念,异常都是在运行的时候捕获处理。
    • 异常捕捉的机制与其他语言中一样,如果有异常发生,catch 子句是按次序捕捉的。因此,在 catch 子句中,越具体的异常越要靠前,越普遍的异常越靠后,如果把越普遍的异常写在前,把具体的异常写在后,在 Scala 中也不会报错,但这样是非常不好的编程风格。
    • finally 子句用于执行不管是正常处理还是有异常发生时都需要执行的步骤,一般用于对象的清理工作,这点和 Java 一样。
    • throw 关键字,抛出一个异常对象。所有异常都是 Throwable 的子类型。throw 表达式是有类型的,就是Nothing,因为 Nothing 是所有类型的子类型,所以 throw 表达式可以用在需要类型的地方。
    • java 提供了 throws关键字来声明异常。可以使用方法定义声明异常。它向调用者函数提供了此方法可能引发此异常的信息。它有助于调用函数处理并将该代码包含在 try-catch块中,以避免程序异常终止。在 Scala 中,可以使用 throws 注解来声明异常。
    	def main(args: Array[String]): Unit = {
    		 f11()
    	}
    	@throws(classOf[NumberFormatException])
    	def f11()={
    		 "abc".toInt
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    示例代码(4):

    object Test_Exception {
      def main(args: Array[String]): Unit = {
          try{
            val n =10 /0;
          }catch {
            case e: ArithmeticException =>{
              println("发生算数异常")
            }
            case e: Exception => {
              println("发生一般异常")
            }
          }finally {
            println("处理结束")
          }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    本次Scala中异常机制分享的内容到这里就结束了,与Java异常机制相比较确实有很多灵活的地方,希望对大家有所帮助!!!

    在这里插入图片描述

  • 相关阅读:
    流程控制语句 流程控制开关
    Verilog刷题[hdlbits] :Always if2
    如何从0开发一个Vue组件库并发布到npm
    【NOI模拟赛】防AK题(生成函数,单位根,Pollard-Rho)
    JavaScript基础知识总结
    RPC协议交互流程
    win10安装onnx、tensorrt(python用,超简单安装版)
    【Netty】九、Netty自定义协议
    JNI编程之java层和native层的数组数据的交互
    机理类模型的建模思路|2021年亚太赛B题|2022备赛|前景目标提取
  • 原文地址:https://blog.csdn.net/Zp_insist/article/details/126024700