जवाबों:
एक विधि "डीईएफ़" एक "शुरुआत" कथन के रूप में काम कर सकती है:
def foo
...
rescue
...
end
do/ endब्लॉक शाब्दिक रूप में अंतर्निहित अपवाद ब्लॉक होते हैं।
rescue TypeError; rescue NameError- या आप कॉमा को अलग कर सकते हैं अपवाद कक्षाएं, जैसेrescue TypeError, NameError
आप इनलाइन बचाव भी कर सकते हैं:
1 + "str" rescue "EXCEPTION!"
बाहर प्रिंट होगा "अपवाद!" चूंकि 'स्ट्रिंग को फिक्सनम में ज़ब्त नहीं किया जा सकता'
StandardErrorअपने सभी उपवर्गों को बचाता है , जैसे NameError- मतलब है कि आपके कोड में एक टाइपो भी एक त्रुटि नहीं बढ़ाएगा। विचार करें ।botbot/don-t-inline-rescue-in- देखें माणिक्य ।
उदाहरण:
begin
# something which might raise an exception
rescue SomeExceptionClass => some_variable
# code that deals with some exception
ensure
# ensure that this code always runs
end
यहाँ, defएक beginबयान के रूप में :
def
# something which might raise an exception
rescue SomeExceptionClass => some_variable
# code that deals with some exception
ensure
# ensure that this code always runs
end