raise ném exception.
Có 3 dạng:
ruby
raise # re-raise exception hiện tại
raise "something went wrong" # RuntimeError với message
raise ArgumentError, "bad value" # explicit class + messageretry khởi động lại begin block từ đầu — chỉ dùng được trong rescue:
ruby
MAX_RETRIES = 3
attempts = 0
begin
attempts += 1
connect_to_api
rescue NetworkError => e
retry if attempts < MAX_RETRIES
raise # hết retry → re-raise
endLưu ý: retry không có giới hạn → vòng lặp vô tận; không reset state → lặp lỗi mãi; retry blind → che giấu bug thực sự. Rule: luôn có counter/limit và chỉ retry với exception biết cách handle.
raise throws an exception.
Three forms:
ruby
raise # re-raise current exception
raise "something went wrong" # RuntimeError with message
raise ArgumentError, "bad value" # explicit class + messageretry restarts the begin block from scratch — only valid inside rescue:
ruby
MAX_RETRIES = 3
attempts = 0
begin
attempts += 1
connect_to_api
rescue NetworkError => e
retry if attempts < MAX_RETRIES
raise # exhausted — re-raise
endPitfalls: no limit → infinite loop; state not reset → same error repeats forever; blind retry → hides real bugs. Rule: always have a counter/limit, and only retry for exceptions you know how to recover from.