Is there a way to rescue all exceptions under a certain namespace?
For example, I want to rescue all of the Errno::* exceptions (Errno::ECONNRESET, Errno::ETIMEDOUT). I can go ahead and list them all out on my exception line, but I was wondering if I can do something like.
begin # my code rescue Errno # handle exception end
The above idea doesn't seem to work, thus is there something similar that can work?
Exception handling is used to handle the exceptions. We can use try catch block to protect the code. Catch block is used to catch all types of exception. The keyword “catch” is used to catch exceptions.
Besides specifying a single exception class to rescue, you can pass an array of exception classes to the rescue keyword. This will allow you to respond to multiple errors in the same way. Multiple rescue blocks can be used to handle different errors in different ways.
Ruby also provides a separate class for an exception that is known as an Exception class which contains different types of methods. The code in which an exception is raised, is enclosed between the begin/end block, so you can use a rescue clause to handle this type of exception.
All the Errno
exceptions subclass SystemCallError
:
Module
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::
.
So you could trap SystemCallError
and then do a simple name check:
rescue SystemCallError => e raise e if(e.class.name.start_with?('Errno::')) # do your thing... end
Here is another interesting alternative. Can be adapted to what you want.
Pasting most interesting part:
def match_message(regexp) lambda{ |error| regexp === error.message } end begin raise StandardError, "Error message about a socket." rescue match_message(/socket/) => error puts "Error #{error} matches /socket/; ignored." end
See the original site for ruby 1.8.7 solution.
It turns out lambda not accepted my more recent ruby versions. It seems the option is to use what worked in 1.8.7 but that's IM slower (to create a new class in all comparisons. So I don't recommend using it and have not even tried it:
def exceptions_matching(&block) Class.new do def self.===(other) @block.call(other) end end.tap do |c| c.instance_variable_set(:@block, block) end end begin raise "FOOBAR: We're all doomed!" rescue exceptions_matching { |e| e.message =~ /^FOOBAR/ } puts "rescued!" end
If somebody knows when ruby removed lambda support in rescue
please comment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With