raise क्या है
raise जानबूझकर alarm बजाता है जब आपका code एक problem ढूंढता है जिसे यह fix नहीं कर सकता। यह 'रुको, कुछ गलत है!' चिल्लाने जैसा है।
In this page:
Syntax
raise "message"
raise ExceptionType, "message"
raise # re-raise current exception
raise
raise explicitly एक exception trigger करता है, या तो एक message के साथ एक नया (raise "something broke") या एक specific exception class (raise ArgumentError, "bad input")।
एक rescue block के अंदर बिना arguments के raise call करना currently caught exception को फिर raise करता है। Methods जो invalid input या state detect करते हैं typically silently एक error value return करने की बजाय raise करते हैं।
Note:
एक rescue block के अंदर बिना arguments के bare
raise currently handled हो रही exception को फिर raise करता है, इसके original details preserve करते हुए।उदाहरण: raise
def check_age(age)
raise ArgumentError, "Age cannot be negative" if age < 0
puts "Age #{age} is valid"
end
begin
check_age(-5)
rescue ArgumentError => e
puts "Error: #{e.message}"
end
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- सिर्फ एक class और एक message को गलत order में
raiseलिखना, जैसेraise "msg", ArgumentError। - बिना message के rescue और re-raise करना, यह hide करते हुए कि यह क्यों fail हुआ।
raiseको एकreturnकी तरह इस्तेमाल करना, जब यह method रोक देता है जब तक rescue न हो।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: