scala中捕获异常

Scala的例外 (Exceptions in Scala)

Exceptions are cases or events that occur in the program at run time and hinder the regular flow of execution of the program. These can be handled in the program itself.

例外是在运行时在程序中发生并阻碍程序正常执行流程的情况或事件。 这些可以在程序本身中处理。

Scala also provides some methods to deal with exceptions.

Scala还提供了一些处理异常的方法。

When the program's logic is probable to throw an exception, it has to be declared that such an exception might occur in the code, and a method has to be declared that catches the exception and works upon it.

当程序的逻辑可能引发异常时,必须声明该异常可能在代码中发生,并且必须声明一个捕获异常并对其进行处理的方法。

如何抛出异常? (How to throw exception?)

In Scala, the throw keyword is used to throw an exception and catch it. There are no checked exceptions in Scala, so you have to treat all exceptions as unchecked exceptions.

在Scala中, throw关键字用于引发异常并捕获它。 Scala中没有检查异常,因此您必须将所有异常视为未检查异常。

Also read, Java | checked and unchecked exceptions

另请阅读Java 已检查和未检查的异常

Syntax:

句法:

    throw exception object

To throw an exception using the throw keyword we need to create an exception object that will be thrown.

要使用throw关键字引发异常,我们需要创建一个将被引发的异常对象。

Example 1:

范例1:

In this example, we will throw an exception if the speed will be above 175KmpH.

在此示例中,如果速度高于175KmpH,则将引发异常。

object MyObject {def checkSpeed(speed : Int ) = {if (speed > 175)
throw new ArithmeticException("The rider is going over the speed Limit!!!")
else
println("The rider is safe under the speed Limit.")
}
def main(args: Array[String]) {checkSpeed(200)
}
}

Output:

输出:

java.lang.ArithmeticException: The rider is going over the speed Limit!!!
...

Explanation:

说明:

In the above code, we have declared a function checkSpeed() that takes the speed of the bike and check if it’s above the speed limit which is 175. If the speed is above 175 it throws an Arithmetic exception that says "The rides is going above the speed limit!!!". Else it prints "The rider is safe under the speed limit".

在上面的代码中,我们声明了一个函数checkSpeed()来获取自行车的速度,并检查其是否超过了175的速度限制。如果速度超过175,则会引发算术异常, 异常表示“骑车正在行驶超过速度限制!!!” 。 否则将打印“在速度限制下骑手很安全”

Example 2:

范例2:

In this example, we will learn to throw an exception and catch it using the try-catch block.

在此示例中,我们将学习引发异常并使用try-catch块捕获该异常。

object MyObject {def isValidUname(name : String ) = {throw new Exception("The user name is not valid!")
}
def main(args: Array[String]) {try {isValidUname("Shivang10");
}
catch{case ex : Exception => println("Exception caught : " + ex)
}
}
}

Output:

输出:

Exception caught : java.lang.Exception: The user name is not valid!

Exception:

例外:

In the above code, we have a function isValidUname() that throws an exception which is caught by the catch block in the main function.

在上面的代码中,我们有一个函数isValidUname() ,该函数引发一个异常,该异常由主函数中的catch块捕获。

翻译自: https://www.includehelp.com/scala/how-to-throw-exception.aspx

scala中捕获异常

scala中捕获异常_如何在Scala中引发异常?相关推荐

  1. scala集合中添加元素_如何在Scala中将元素添加到列表中?

    scala集合中添加元素 In Scala, lists are immutable data structures in which adding new elements is not allow ...

  2. scala 转换为字符串_如何在Scala中将字符串转换为布尔值?

    scala 转换为字符串 String in Scala is a sequence of characters. In Scala, the String object is immutable. ...

  3. typescript中函数_如何在TypeScript中合成Canvas动画

    typescript中函数 by Changhui Xu 徐昌辉 如何在TypeScript中合成Canvas动画 (How to Compose Canvas Animations in TypeS ...

  4. scala字符串替换_如何在Scala中替换字符串中的正则表达式模式?

    scala字符串替换 Scala | 替换字符串中的正则表达式模式 (Scala | Replacing a regular expression pattern in a string) Repla ...

  5. scala集合中添加元素_如何在Scala中获得列表的第一个元素?

    scala集合中添加元素 清单 (List) A list is a linear data structure. It is a collection of elements of the same ...

  6. scala 空列表_如何在Scala中展平列表列表?

    scala 空列表 Flattening of List is converting a list of multiple List into a single List. To flatten Li ...

  7. m 文件 dll matlab 中调用_如何在matlab中调用python程序

    现在python很火,很多代码都是python写的,如果你和我一样,习惯了使用matlab,还想在matlab中调用Python的代码,应该怎么办呢?其中一条思路:首先在matlab中调用系统脚本命令 ...

  8. javascript中索引_如何在JavaScript中找到数字在数组中所属的索引

    javascript中索引 Sorting is a very important concept when writing algorithms. There are all kinds of so ...

  9. error:lnk2005 已经在*.obj中定义_如何在 Spring 中自定义 scope

    大家对于 Spring 的 scope 应该都不会默认.所谓 scope,字面理解就是"作用域"."范围",如果一个 bean 的 scope 配置为 sing ...

最新文章

  1. 第四回 基类中的修饰符,应该根据你对架构的理解去定义它们,没有绝对的
  2. LAMP 全功能编译安装 for CentOS6.3笔记(更新)
  3. 使用命令将单个java文件打包为jar
  4. JQuery 判断滚动条是否到底部
  5. creo动画如何拖动主体_Animate如何制作动态遮罩文字动画
  6. ssh框架常见错误与解决方法
  7. flutter中使用InkWell给任意Widget添加点击事件
  8. Python使用BoundedSemaphore对象进行线程同步
  9. 今日恐慌与贪婪指数为40 恐慌程度有所上升
  10. 使用 vs 2008 宏制作自动注释工具
  11. GIS_gdal geotiff文件与JAVA 浮点二维数组array之间的转换
  12. DOM之节点操作总结(附实例、图解)
  13. 【今天带大家用Python来制作一个自动抢票的脚本小程序】
  14. IE浏览器老是假死怎么办 IE假死的解决办法
  15. 袋鼠云数据湖平台「DataLake」,存储全量数据,打造数字底座
  16. 大寰机器人通讯转换系统(CTS-B1.0) 操作说明
  17. php 英文替换中文,php如何中英文符号替换?
  18. Accumulation Degree
  19. Java基础 DAY02
  20. pyecharts显示K线、均线、成交量和MACD

热门文章

  1. oracle 10g rac 修改sga_target不生效,Oracle Rac 修改SGA_TARGET值无变化
  2. 导入ansys的实体怎么进行parameter_ANSYS在线缆线束设计中的仿真应用
  3. tensorflow适用于python版本_tensorflow用python哪个版本更好?
  4. 五笔字型键盘字根图_手机输入法的派别之争:九宫格和全键盘,哪个更科学?...
  5. 日是这一年的等几天Java代码_java中计算指定日期是一年的第几天的方法
  6. python编程制作接金币游戏_一个简单的pygame接金币游戏
  7. HDL的综合和c语言的编译区别,C语言与verilog 的区别及相互转化
  8. java电子通讯录毕业设计_(C)JAVA001电子通讯录(带系统托盘)
  9. LDAP命令介绍---import-ldif
  10. 猫哥教你写爬虫 005--数据类型转换-小作业