← Back to Ruby Course | Chapter 11: File I/O & Exceptions | Lesson 4 of 6

raise क्या है

raise जानबूझकर alarm बजाता है जब आपका code एक problem ढूंढता है जिसे यह fix नहीं कर सकता। यह 'रुको, कुछ गलत है!' चिल्लाने जैसा है।

In this page:

  1. raise
Syntax
ruby
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

markup
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
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. #}
आम गलतियां
  1. सिर्फ एक class और एक message को गलत order में raise लिखना, जैसे raise "msg", ArgumentError।
  2. बिना message के rescue और re-raise करना, यह hide करते हुए कि यह क्यों fail हुआ।
  3. raise को एक return की तरह इस्तेमाल करना, जब यह method रोक देता है जब तक rescue न हो।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.