ruby 怎么抛异常

Ruby异常处理 (Ruby Exception Handling)

Exceptions are abnormal conditions arising in the code segment at runtime in the form of objects. There are certain predefined Exception classes whereas we can create our exception known as Custom exception. Accepts distorts the normal flow of the program at runtime. Exception handling is the process of handling such abnormal conditions or you can also refer them as unwanted events. There is a distinct class known as Exception class in Ruby which contains method specially defined to deal with the unexpected events.

异常是在运行时在代码段中以对象形式出现的异常情况。 有某些预定义的Exception类,而我们可以创建称为Custom异常的异常。 接受会在运行时扭曲程序的正常流程。 异常处理是处理此类异常情况的过程,或者您也可以将其称为有害事件。 Ruby中有一个独特的类称为Exception类,其中包含专门定义用于处理意外事件的方法。

To handle an exception, you can take help from raise and rescue block. The general syntax of Exception handling is given below:

处理异常 ,您可以从引发和营救块获得帮助。 异常处理的一般语法如下:

    begin
raise
# block where exception raise
rescue
# block where exception rescue
end

First let us see a code which is creating an abnormal condition:

首先让我们看一下创建异常情况的代码:

a = 12
b = a/0
puts "Hello World"
puts b

The above Ruby code will give you the following error:

上面的Ruby代码将给您以下错误:

check.rb:7:in '/': divided by 0 (ZeroDivisionError)

You can observe that when an Exception is raised, it disrupts the flow of instructions and statements will not execute which are written after the same statement having Exception.

您可以观察到,引发Exception时,它会中断指令流,并且不会执行在具有Exception的同一条语句之后编写的语句。

We can modify the above code in the following way to avoid the execution distortion of the rest of the instructions.

我们可以通过以下方式修改上述代码,以避免其余指令的执行失真。

=begin
Ruby program to show Exception Handling.
=end
begin
a = 12
raise
b = a/0
rescue
puts "Exception rescued"
puts "Hello World"
puts b
end

Output

输出量

Exception rescued
Hello World

In the above program, you can observe that we are writing the code inside the begin...end block. We have put the statement inside "raise" which may raise any kind of abnormality. We have used rescue statement to handle that exception. You can see that the exception raised is not affecting the rest of the instruction. We are getting "Hello World" printed at the end.

在上面的程序中,您可以观察到我们正在在begin ... end块内编写代码。 我们将语句放在“ raise”中 ,这可能引起任何异常。 我们已经使用了抢救声明来处理该异常 。 您可以看到引发的异常不会影响指令的其余部分。 我们将在最后打印“ Hello World”

We can create our Exception known as Custom Exception with the help of any user defined method. Let us one of its examples for a better understanding of the implementation:

我们可以创建例外称为自定义异常与任何用户定义的方法的帮助。 让我们为更好地理解实现示例之一:

=begin
Ruby program to show Exception Handling.
=end
def exception_arised
begin
puts 'Hello! Welcome to the Includehelp.com.'
puts 'We are still safe from Exception'
# using raise to generate an exception
raise 'Alas! Exception Generated!'
puts 'After Exception created'
# using Rescue method to handle exception
rescue
puts 'Hurrah! Exception handled! We are safe now'
end
puts 'We are out of begin!'
end
# invoking method
exception_arised

Output

输出量

Hello! Welcome to the Includehelp.com. We are still safe from Exception
Hurrah! Exception handled! We are safe now
We are out of begin!

In the above code, we are creating our exception with the help of a user-defined method 'exception_arised'. We are then invoking it. The above is an example of Custom exception.

在上面的代码,我们正在创造我们的“exception_arised”用户定义的方法的帮助下除外 。 然后,我们正在调用它。 以上是Custom异常示例

翻译自: https://www.includehelp.com/ruby/exception-handling-in-ruby.aspx

ruby 怎么抛异常

ruby 怎么抛异常_Ruby中的异常处理相关推荐

  1. java 异常处理发生异常_Java中的异常处理

    java 异常处理发生异常 Exception Handling in Java is a very interesting topic. Exception is an error event th ...

  2. mysql游标遍历中sql语句出现异常_MySQL中的异常处理,游标

    一.异常处理的理解 ERROR 1062 (23000): Duplicate entry '2' for key 'PRIMARY' 错误 4位error code(5位sql statis):错误 ...

  3. python 打开本地程序发生异常_Python中的异常处理

    Python中的异常事件: 当Python遇到无法正常处理的事件时,便是异常发生的时候,Python将异常也当作一个对象,尽管他是出错的.当发生异常时候我们要捕获他,否则程序就会中断运行. Pytho ...

  4. java 抛异常给上级_java异常处理机制(示例代码)

    Exception 类的层次 java中所有的异常类是从 java.lang.Exception 类继承的子类. 而Exception 类是 Throwable (可抛出的)类的子类.除了Except ...

  5. c# 溢出抛异常_Rust竟然没有异常处理?

    学习Rust最好的方法,就是和其他主流语言,比如Java.Python进行对比学习.不然怎么能get到它的特别呢? 1. 主流模式:try-catch-finally 基本上,当你学会了某种语言的tr ...

  6. java事务抛异常_java中抛异常后如何使事务回滚

    spring声明式事务管理默认对非检查型异常和运行时异常进行事务回滚,而对检查型异常则不进行回滚操作 代码中try--catch抛出的Exception异常,属于检查型异常,Spring的框架默认是不 ...

  7. java遇到无穷大抛异常_Java中一个for语句导致无穷大死循环的例子

    在Java开发中常用到For循环,它对简化业务处理,提高效率,非常有帮助.但要防止程序算法中可能导致死循环的情况,而且有的死循环还不好察觉.比如下面这个例子,算法极容易认为是50,实际上是无穷大的一个 ...

  8. kotlin协程硬核解读(5. Java异常本质协程异常传播取消和异常处理机制)

    版权声明:本文为openXu原创文章[openXu的博客],未经博主允许不得以任何形式转载 文章目录 1. 异常的本质 1.1 操作系统.程序.JVM.进程.线程 1.2 异常方法调用栈 1.3 ja ...

  9. kotlin中的异常处理_如何使用assertFailsWith在Kotlin中测试异常

    kotlin中的异常处理 by Daniel Newton 丹尼尔·牛顿 如何使用assertFailsWith在Kotlin中测试异常 (How to test exceptions in Kotl ...

最新文章

  1. 微信开发之自动回复图文消息
  2. MySQL5.6 主从复制配置
  3. HTML5 Canvas动画效果实现原理
  4. android Button源码分析
  5. Introduction to Big Data with Apache Spark 课程总结
  6. beego ajax图片上传,Beego框架POST请求接收JSON数据
  7. python语言的重要性_【一点资讯】这个可能打败Python的编程语言,正在征服科学界 www.yidianzixun.com...
  8. ad17如何删除3d实体_3D打印的过程/流程
  9. 海康VisionMaster定位任务
  10. 用友nc系统服务器端口号,用友NC数据库服务器参数配置说明
  11. 第三届易观算法大赛 -- OLAP Session分析(5万奖金)
  12. memory allocator php,LNMP的安装
  13. Sqlserver的身份验证模式
  14. 转载:SpringBoot非官方教程 | 第二十四篇: springboot整合docker
  15. matlab分析具体问题论文,关于Matlab论文范文写作 Matlab在化探异常解释评价中应用相关论文写作资料...
  16. RCTF 2018线上赛 writeup
  17. winmail中web界面破除用户限制
  18. ubuntu下载谷歌云盘大文件
  19. 摆平中文搜索引擎的分词错误(转)
  20. 黑道圣徒3重制版如何设置中文

热门文章

  1. oracle 计算中位数,SQL 如何计算每个分组的中位数
  2. android 布局防抖动,Android全屏返回布局抖动问题
  3. android 使用c 代码实现,JNI开发实现helloworld,调用自己的C代码实现(1)
  4. 网页变成灰色,对重大事件表示哀悼
  5. jq绑定的事件不生效
  6. linux里的进程简介
  7. ASP.NET Web API 数据提供系统相关类型及其关系
  8. 计算机在盲童音乐教学中的具体应用,计算机在高校中的具体应用
  9. php 工资 2018,佛山市2018年平均工资(社平工资)
  10. php 状态码302,HTTP状态码302、303和307的故事