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.pswp.cn/news/377389.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377389.shtml
英文地址,請注明出處:http://en.pswp.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;并且一直…