在ruby中使用正确的异常子类 [英] Using the right exception subclass in ruby

查看:84
本文介绍了在ruby中使用正确的异常子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以访问ruby的异常层次结构(在镐和蜂鸟中都有提到),但是我不知道使用哪个异常,因为我没有找到任何关于每个术语的含义的信息。 p>

使用正确的异常类吗?

解决方案

这取决于 。



Ruby的主要问题之一是缺乏良好的规范。过去更糟糕的是,



这主要是一个风格的问题:如果你的错误可以用一个内置的异常类描述得好,就可以使用它。如果您认为子类有意义,请使用它。



否则,您可以考虑遵循这个来自C ++编码标准的引导


创建非常精细的异常
层次是浪费时间。没有人b $ b结束关心,所有的努力,
浪费。相反,为每个库或命名空间创建一个
异常,

异常中有异常原因,以指示
异常的类型。



例如,对于您的操作系统封装
libary,创建一个名为
OsencapException的异常。



I have access to ruby's exception hierarchy (it's mentioned in both the pickaxe and the hummingbird), but I'm not sure which exception to use because I haven't found any information on what each of the terms mean.

Does using the right exception class matter?

解决方案

"It depends".

One of the main problems with Ruby is the lack of good specification. It used to be worse, though.

This is mainly a question of style: If your error can be described well with one of the builtin exception classes, use it. If you think a subclass makes sense, use it.

Otherwise, you could consider to follow this lead from a C++ coding standard:

Creating very elaborate exception hierarchies is a waste of time. Nobody ends of caring and all the effort goes to waste. Instead, create one exception per library or namespace and have an exception reason within that exception to indicate the type of the exception.

For example, for your OS encapsulation libary, make an exception called OsencapException.

这篇关于在ruby中使用正确的异常子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆