我正在try 将一些Python代码转换成Ruby.Ruby中是否有与Python中的try语句等价的语句?

推荐答案

举个例子:

begin  # "try" block
    puts 'I am before the raise.'  
    raise 'An error has occurred.' # optionally: `raise Exception, "message"`
    puts 'I am after the raise.'   # won't be executed
rescue # optionally: `rescue Exception => ex`
    puts 'I am rescued.'
ensure # will always get executed
    puts 'Always gets executed.'
end 

Python中的等效代码是:

try:     # try block
    print('I am before the raise.')
    raise Exception('An error has occurred.') # throw an exception
    print('I am after the raise.')            # won't be executed
except:  # optionally: `except Exception as ex:`
    print('I am rescued.')
finally: # will always get executed
    print('Always gets executed.')

Python-3.x相关问答推荐

使用Python装载. iso文件

如何从Django连接到Neo4J s AuraDB(免费层)?'

小部件padx和包方法ipadx有什么不同?

在 sum() 中将字符串转换为 int (或 float)

为什么 tkinter 在 tkinter 窗口外计算鼠标事件?

在Python中基于组/ID将两个数据帧进行映射,找出较接近的值

Django - ValueError:无法将字符串转换为浮点数:''

在 pytest 中,如何测试 sys.exit('some error message')?

为什么 numpy 的 `np.char.encode` 会将一个空的 unicode 数组变成一个空的 `float64` 数组?

Python ** 用于负数

如何在 VSCode 的在 Cloud Run Emulator 上运行/调试构建设置中添加 SQL 连接

Python从base64转换为二进制

Visual Studio Code 中的 Python 3.x 类型提示

如何注释一个以另一个函数作为参数的函数?

是否有与 Laravel 4 等效的 python?

如何调试垂死的 Jupyter Python3 内核?

aiohttp+sqlalchemy:在回滚无效事务之前无法重新连接

Python configparser 不会接受没有值的键

根据条件过滤元组列表

如何使用 python 库连接到 poloniex.com websocket api