- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: ruby-best-practices/no-rescue-exception
Language: Ruby
Severity: Notice
Category: Best Practices
The rule “Do not rescue the Exception class” is a crucial practice in Ruby programming for handling exceptions. The Exception class is the root of Ruby’s exception hierarchy, so when you rescue Exception, you’re potentially catching and handling severe system errors that Ruby itself is trying to bubble up. These could be fundamental issues like memory overflows and syntax errors, which could cause the program to behave unexpectedly or even crash.
Rescuing the Exception class can lead to major problems in debugging since it can hide the true nature of the error and its source. It makes it harder to pinpoint where and why the error occurred. This can lead to significant delays in identifying and resolving coding issues.
Instead of rescuing the Exception class, it is better to rescue more specific error classes or use StandardError
which is the superclass for most error types. For instance, if you’re expecting possible nil values, use rescue NoMethodError
. This allows Ruby to handle severe system errors appropriately and ensures that you’re only rescuing the errors you expect. This practice makes your code safer, more predictable, and easier to maintain and debug.
begin
# The exit will be rescued, so the program won't exit.
exit
rescue Exception
# You are here!
end
begin
# The exit will be rescued, so the program won't exit.
exit
rescue Exception => e
# You are still here!
end
begin
exit
rescue StandardError
# Not reached
end
begin
exit
rescue StandardError => e
# Not reached either
end
begin
exit
rescue => e
# Equivalent to the above.
# Does not rescue Exception, but StandardError.
end