Ruby begin/rescue/ સાથે errors સંભાળે છે (Ruby નું try/catch/finally ની સમકક્ષ). તમે exceptions કરો અને તેમને કરો, cleanup માટે સાથે. Ruby method-level rescue પણ અમલમાં લાવે છે (સ્પષ્ટ વિના) સ્વચ્છ કોડ માટે.
Ruby begin/rescue/ સાથે errors સંભાળે છે (Ruby નું try/catch/finally ની સમકક્ષ). તમે exceptions કરો અને તેમને કરો, cleanup માટે સાથે. Ruby method-level rescue પણ અમલમાં લાવે છે (સ્પષ્ટ વિના) સ્વચ્છ કોડ માટે.
ensureraiserescueensurebeginbegin
result = risky_operation
rescue ArgumentError => e # rescue a SPECIFIC exception type
puts "Bad argument: #{e.message}"
rescue StandardError => e # broader catch (StandardError, not Exception)
puts "Error: #{e.message}"
retry if attempts < 3 # retry can re-run the begin block
else
puts "succeeded" # runs if NO exception
ensure
cleanup # ALWAYS runs (success or failure)
end
begin/rescue/ensure Ruby નું try/catch/finally છે. ચોક્કસ પ્રકારોને rescue કરો (rescue ArgumentError), guaranteed cleanup માટે ensure વાપરો, અને no-error case માટે else. retry બ્લોકને ફરી પ્રયાસ કરી શકે છે.
rescue => e # ✅ bare rescue catches StandardError (the right default)
rescue StandardError # ✅ explicit, same thing
rescue Exception # ❌ AVOID — catches EVERYTHING including system signals
# (Interrupt/Ctrl-C, SystemExit) — can break the program
મહત્વપૂર્ણ: એક શુદ્ધ rescue StandardError પકડે છે (application errors માટે સાચો ડિફોલ્ટ). Exception (root) rescue કરવું બધું પકડે છે, જેમાં system-level signals જેમ કે Interrupt (Ctrl-C) અને SystemExit શામેલ છે — જે તમે સામાન્યતः પકડવા માંગતા નથી, કારણ કે તે program ને interrupt થવાથી અથવા યોગ્યતાથી બહાર નીકળવાથી રોક શકે છે. Ruby માં સામાન્ય ભૂલ.
raise ArgumentError, "Amount must be positive" # raise a built-in
raise "Something failed" # raises RuntimeError
# custom exception class
class InsufficientFundsError < StandardError # inherit from StandardError
def initialize(msg = "Not enough funds")
super
end
end
raise InsufficientFundsError
def process
do_work
rescue => e # rescue WITHOUT begin — applies to the whole method body
handle(e)
ensure
cleanup
end
યોગ્ય exception handling robust Ruby applications માટે આવશ્યક છે, અને Ruby ના અભિગમ — તેના ચોક્કસ idioms અને સાવચેતીઓ સહિત — સમજવું મહત્વપૂર્ણ છે.
begin/rescue/ensure માળખું (Ruby નું try/catch/finally), ચોક્કસ exception પ્રકારો rescue કરવું, guaranteed cleanup માટે ensure વાપરવું, અને સ્વચ્છ method-level rescue (સ્પષ્ટ begin વિના) મૂળભૂત દૈનિક જ્ઞાન છે.
વિશેષ રીતે મહત્વપૂર્ણ અને વિશિષ્ટ Ruby બિંદુ StandardError vs Exception તફાવત છે: એક શુદ્ધ rescue (અને સાચો ડિફોલ્ટ) StandardError પકડે છે, પરંતુ Exception (hierarchy ના root) rescue કરવું બધું પકડે છે — Interrupt (Ctrl-C) અને SystemExit જેવા system signals સહિત — જે તમે લગભગ ક્યારેય ચાહતા નથી, કારણ કે તે program ને interrupt થવાથી અથવા યોગ્યતાથી બહાર નીકળવાથી રોક શકે છે, અને આ એક સામાન્ય, નુકસાનકારક ભૂલ છે.
આ સમજવું કે તમે application errors માટે StandardError rescue કરવું જોઈએ (Exception નહીં) Ruby-specific જ્ઞાન છે જે વાસ્તવિક bugs અટકાવે છે.
માળખું, custom exception classes (StandardError થી inherit કરવું), retry, method-level rescue idiom, અને especially StandardError-not-Exception caveat જાણવું robust, સાચું Ruby લખવા માટે મહત્વપૂર્ણ છે જે errors ને gracefully સંભાળે છે program ની interrupt અથવા exit કરવાની ક્ષમતા તોડ્યા વિના.
અનહ ન થયેલ errors અને Exception-rescuing ભૂલ બંને વાસ્તવિક સમસ્યાઓ સર્જે છે, Ruby ના exception handling ને માસ્ટર કરવું — તેનું માળખું, idioms, અને critical StandardError vs Exception તફાવત — reliable Ruby development અને એક સામાન્ય interview topic માટે મહત્વપૂર્ણ, વારંવાર પ્રસંગોપાત જ્ઞાન છે.