ruby 怎么抛异常_Ruby中的异常处理

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 怎么抛异常

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/377389.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

cocos2d-x游戏开发系列教程-中国象棋02-main函数和欢迎页面

之前两个博客讲述了象棋的规格和工程文件之后,我们继续深入的从代码开始学习cocos2dx首先从程序入口main函数开始main函数int APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow) {UNREFERENCED_PARAMETER(h…

[原创]Android中的android:layout_width和android:width的区别

在android系统中&#xff0c;我们可以通过在xml资源文件中定义布局&#xff0c;一般的写法是&#xff1a; <LinearLayout xmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_parent"android:layout_height"ma…

【C++基础】模板参数与模板继承

模板参数 默认类型参数 函数参数可以设定一个默认值&#xff0c;我们现在可以对类模板的类型参数设定一个默认类型。 指定泛型Stack的默认类型参数为 int template<typename T int> class Stack{... };当我们这样定义一个对象时&#xff1a; Stack<> stack;使…

UNIX标准化及实现之POSIX标准可选头文件

POSIX标准定义的可选头文件 头文件说明<aio.h>异步I/O<mqueue.h>消息队列<pthread.h>线程<sched.h>执行调度<semaphore.h>信号量<spawn.h>实时spawn接口<stropts.h>XSI STREAMS接口<trace.h>事件跟踪转载于:https://www.cnblo…

Julia中的denominator()函数

Julia| 分母()函数 (Julia | denominator() function) denominator() function is a library function in Julia programming language, it is used to get the denominator of the rational representation of the given value. denominator()函数是Julia编程语言中的库函数&a…

【C++基础】STL迭代器

已知&#xff1a; STL组成部分&#xff1a; 容器、迭代器、算法、函数对象、空间分配器 容器&#xff1a;用于保存一组数据&#xff0c;数据个体被称为元素 迭代器&#xff1a;用于遍历容器中的元素&#xff0c;容器都有自己专属的迭代器&#xff0c;只有容器才知道如何遍历自己…

用ie9浏览器若出现看视频有声音没图像的问题处理

当我们在用ie9浏览器上网想看视频时&#xff0c;有时会遇到各种问题&#xff0c;尤其是有关声音和图像的。有时候有声音没图像&#xff0c;但有时候有图像却没声音。各种问题。当遇到某些问题时&#xff0c;只要是关于网页视频的&#xff0c;一般都会选择更新网页视频播放插件&…

java架构师之路:JAVA程序员必看的15本书的电子版下载地址

java架构师之路&#xff1a;JAVA程序员必看的15本书的电子版下载地址 作为Java程序员来说&#xff0c;最痛苦的事情莫过于可以选择的范围太广&#xff0c;可以读的书太多&#xff0c;往往容易无所适从。我想就我自己读过的技术书籍中挑选出来一些&#xff0c;按照学习的先后顺序…

office数据集dslr_DSLR的完整形式是什么?

office数据集dslrDSLR&#xff1a;数码单镜反光 (DSLR: Digital Single-Lens Reflex) DSLR is an abbreviation of digital single-lens reflex. It alludes to a digital camera which with the sensor of digital imaging merges optics and mechanism of single-lens reflex…

envs\TensorFlow2.0\lib\site-packages\tensorflow\python\framework\dtypes.py:516: FutureWarning 解决方案

import tensorflow后的完整报错&#xff1a; D:\Anaconda3\envs\TensorFlow2.0\lib\site-packages\tensorflow\python\framework\dtypes.py:516: FutureWarning: Passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will b…

转义序列

转义序列描述\n换行符\r回车\t水平制表符\\反斜杠\$美元符\"双引号\[0-7]{1.3}八进制记法\x[0-9A-Fa-f]{1,2}十六进制记法转载于:https://www.cnblogs.com/cindylu520/archive/2012/07/05/2577246.html

Java动态代理模拟spring的AOP

广州疯狂软件学院拥有三大课程体系包括&#xff1a;java课程&#xff0c;android课程&#xff0c;ios课程&#xff0c;疯狂软件年终钜惠&#xff0c;报名java就业班&#xff0c;免费赠送基础班&#xff0c;名额有限&#xff0c;本月火热报名中&#xff0c;欢迎有志之士电话或者…

xlrd.biffh.XLRDError: Excel xlsx file; not supported解决方法

将原本的xlrd卸载&#xff0c;安装旧版本&#xff1a; pip uninstall xlrd pip install xlrd1.2.0转自&#xff1a;https://www.cnblogs.com/xiaoqiangink/p/14144517.html

生产消费是什么设计模式_快速消费品的完整形式是什么?

生产消费是什么设计模式快消品&#xff1a;快速消费品 (FMCG: Fast-Moving Consumer Goods) FMCG is an abbreviation of Fast-Moving Consumer Goods which are also known as Consumer Packed Goods (CPG). These consumer packed goods allude to the products that are sol…

分类释义概述

分类(classification) 是人工智能领域基本的研究领域之一&#xff0c;也是知识表示和获取的主要途径之一。一般认为&#xff0c;分类属于科学发展的较初级阶段&#xff0c;即形成理论之前的阶段。 分类的释义&#xff1a; 中文解释&#xff1a;分类指的是将无规律的事物按照其性…

占位博客

占位博客 转载于:https://www.cnblogs.com/CharmingDang/p/9663895.html

通过从全局和类内部重载operator new /delete来获取内存管理权

目录1、通过重载获得内存管理权2、容器的内存管理3、重载new、array new、replacement new&#xff0c;接管内存控制权1、重载全局::operator new / ::operator delete以及array版本2、在类里面去重载1、通过重载获得内存管理权 之前的几章学习&#xff0c;是红色的路线。此时…

sml完整形式_教资会的完整形式是什么?

sml完整形式教资会&#xff1a;大学教育资助委员会 (UGC: University Grants Commission) UGC is an abbreviation of the University Grants Commission. It is an organization established by the Indian Union government in agreement with the UGC Act 1956 under the Mi…

ASP.NET线程相关配置

1、ASP.NET 同一时刻只能发起的工作线程数量&#xff1a; (maxWorkerThreads * CPU逻辑数量&#xff09;-minFreeThreads 比如2个CPU默认配置maxWorkerThreads100&#xff0c;minFreeThreads176&#xff0c;则同时最大只能有24个工作线程。&#xff08;这里不管 <system.ne…

Android 编程下 AlarmManager

对应 AlarmManager 有一个 AlarmManagerServie 服务程序&#xff0c;该服务程序才是正真提供闹铃服务的&#xff0c;它主要维护应用程序注册的各类闹铃并适时的设置即将触发的闹铃给闹铃设备 ( 在系统中&#xff0c;Linux 实现的设备名为 ”/dev/alarm” ) &#xff0c;并且一直…