Ruby Exception
begin
#可能发生异常的地方
rescue
#如何处理异常
end
rescue,哈哈,太有爱的一个单词了。。。
begin
#可能发生异常的地方
rescue => exception
#如何处理异常
end
这样一来,exception就可以用来存放异常对象了。
$!:用来存放最后一次发生的异常对象
$@:异常发生的位置等信息。
异常对象的方法:
class:例外的类别
message: 例外的消息
backtrace: $@等同于$!.backtrace,异常发生的位置信息
更加完善的异常处理
begin
#process
rescue => ex
#process on ex
ensure
#无论如何都会执行的动作
end
※在ensure中可以使用retry,他会重新去执行begin中的内容,如果没有强制退出则一直执行。
rescue可以像if, unless一样使用如下形式:
n = Integer(val) rescue 0 #跟上面同样的效果 begin n = Integer(val) rescue n = 0 end
如果val不是一个数字类型的字符串的话,n就等于0。
按照异常分类处理
begin
#process
rescue Exception1, Exception2 => ex
#process on ex
rescue Exception3, Exception4 => ex2
#process on ex2
rescue => ex3
#process on other kinds of exceptions
end
StandardError
这个类是Exception的自类,Exception还有其他的子类。但是rescue补救的知识StandardError类或者是其子类。
于是我们设计自己的异常的时候,需要首先继承这个StandardError。
class VoctralsError < StandardError; end
然后,其它的类可以继承这个VoctralsError类。
class Error1 < VoctralsError; end
引发异常raise
raise "Error Information" #产生一个RuntimeError,"Error Information"作为异常的message;
raise SomeException #产生一个SomeException
raise SomeException, "Error Information" #产生一个SomeException,并设定其message为"Error Information"
raise
1)写在rescue之外,直接引发一个RuntimeError;
2)写在rescue内,再次引发导致该rescue发生的异常($!)。
throw和catch
catch(:some){
#something before a :some throw
throw :some
#something after a :some throw
}
catch能够保证:some之前的代码顺利进行,当出现:some被throw之后,后面的代码不会执行。
看起来类似if。
但是可以用这个来打断一个多层循环:
catch(:test){
loop{
loop{
...
if val != 0
throw :test, val
end
}
}
throw有两个参数的时候,第二个参数作为catch的返回值。
Ruby Exception的更多相关文章
- ruby : Exception Notification
https://github.com/smartinez87/exception_notification#sections Add the following line to your applic ...
- 《ruby编程语言》笔记2 对象
ruby是一门非常纯粹的面向对象的语言:所有值都是对象,而且没有基本类型(primitive type)和对象类型的区别,这一点不同于其他语言.在Ruby中,所有对象都继承一个Object类,而且共享 ...
- 【原创】大数据基础之Logstash(3)应用之file解析(grok/ruby/kv)
从nginx日志中进行url解析 /v1/test?param2=v2¶m3=v3&time=2019-03-18%2017%3A34%3A14->{'param1':' ...
- 1月10日 ruby基础教程,查漏补缺; 2月22日 Exception补充
https://ruby-doc.org/core-2.5.0/Exception.html 1月20日练习完1,2章. 第一章 初探 ‘’单引号不执行转义符. \t 制表符.\n 换行符. p me ...
- Ruby的require相关知识
1. 在调用require xxx之前,需要确定xxx这个gem已经安装过了(使用gem install xxx,安装位置可以使用gem env列出),或者xxx是Ruby内置的标准函数库(StdLi ...
- ruby + watir 自动化上传图片文件解决方案
watir自动化捕获上传图片元素: require 'watir' include Watir require 'test/unit' class TC_recorded < Test::Uni ...
- ruby Errors & Exceptions
When you first started coding, errors were probably the last thing you wanted to see. After all, it’ ...
- 【ruby】ruby基础知识
Install Ruby(安装) For windows you can download Ruby from http://rubyforge.org/frs/?group_id=167 for L ...
- Ruby 多线程探索实践与归纳总结
Ruby 多线程 每个正在系统上运行的程序都是一个进程.每个进程包含一到多个线程. 线程是程序中一个单一的顺序控制流程,在单个程序中同时运行多个线程完成不同的工作,称为多线程. Ruby 中我们可以通 ...
随机推荐
- magento如何获取某一产品的订单量代码
<?php $sku = nl2br($_product->getSku()); $_productCollection = Mage::getResourceModel('reports ...
- 第二个Sprint冲刺第九天
讨论地点:宿舍 讨论成员:邵家文.李新.朱浩龙.陈俊金 工作:接着昨天的任务
- dede cms列表页调用文章简介(借鉴)
{dede:list pagesize='8' infolen='50'}<a href="[field:arcurl/]">[field:title/][内容调用代码 ...
- Linux中的likely()和unlikely()
likely()与unlikely()在2.6内核中,随处可见,那为什么要用它们?它们之间有什么区别呢?首先明确: if (likely(value))等价于if (value) if (unlike ...
- hadoop问题锦集(一)-搭建集群环境时的常见问题
1.没有主机的路由 1.namenode与datanode之间ping不通了 2.防火墙得关闭: ufw status ufw disabled 2. ssh localhost ssh:connec ...
- (基础篇) php中0与空 Null false的区别
<?php $test=0; if($test==''){ echo '<br />在php中,0即为空'; //被输出 } if($test===''){ echo '<br ...
- DPM(Deformable Parts Model)--原理(一)(转载)
DPM(Deformable Parts Model) Reference: Object detection with discriminatively trained partbased mode ...
- jq中的三元运算结构
三元运算的结构为:Boolean?值1:值2.它的第一个参数必须为布尔值.
- PHP 的 HMAC_SHA1算法 实现
根据RFC 2316(Report of the IAB,April 1998),HMAC(散列消息身份验证码: Hashed Message Authentication Code)以及IPSec被 ...
- 控制文本和外观------CSS Binding(CSS类名绑定)
<style> .myColor{color:red;font-size:13px} .yourColor{color:green;font-size:34px}</style> ...