有没有办法拯救某个命名空间下的所有异常?
@H_403_2@例如,我想拯救所有的Errno :: *异常(Errno :: ECONNRESET,Errno :: ETIMEDOUT).我可以继续在我的异常行上列出所有的东西,但是我想知道我是否可以做某事.
begin # my code rescue Errno # handle exception end@H_403_2@上面的想法似乎不起作用,因此有什么类似的可以工作吗?
解决方法
所有
@L_404_0@:
@H_403_2@Module@H_403_2@所以你可以捕获SystemCallError,然后做一个简单的名字检查:Errno
is created dynamically to map these operating system errors to Ruby classes,with each error number generating its own subclass ofSystemCallError
. As the subclass is created in moduleErrno
,its name will startErrno::
.
rescue SystemCallError => e raise e if(e.class.name.start_with?('Errno::')) # do your thing... end