En bref. Les exceptions Ruby forment un arbre, et une frontière commande tout : StandardError (le rattrapable, et tes erreurs) d’un côté, les signaux et la sortie du programme de l’autre.
Le modèle en une phrase
Hérite de
StandardError, rattrapeStandardError, jamaisException: sinon tu avales le Ctrl-C.
In short. Ruby exceptions form a tree, and one boundary rules everything: StandardError (the rescuable branch, and your errors) on one side, signals and program exit on the other.
The model in one sentence
Inherit from
StandardError, rescueStandardError, neverException: otherwise you swallow Ctrl-C.
rescue # = rescue StandardErrorraise PaiementRefuseraise Interrupt # Ctrl-C
rescue # = rescue StandardError— Unrescuenu ne couvre queStandardErroret ses descendants.A barerescueonly coversStandardErrorand its descendants.raise PaiementRefuse— Ton erreur hérite d'AppError < StandardError: elle tombe dans la zone.Your error inherits fromAppError < StandardError: it falls inside the zone.raise Interrupt # Ctrl-C—Interruptest hors zone : le Ctrl-C traverse lerescueet arrête le programme, comme il se doit.Interruptis outside the zone: Ctrl-C passes through therescueand stops the program, as it should.
La preuve, avec Ruby 3.4
class AppError < StandardError; end
class PaiementRefuse < AppError; end
p PaiementRefuse.ancestors.first(5)
# => [PaiementRefuse, AppError, StandardError, Exception, Object]
p Interrupt.superclass # => SignalException
p SignalException.superclass # => ExceptionLe rescue nu laisse passer le Ctrl-C
begin
begin; raise Interrupt; rescue => e; puts "rescue nu a pris"; end
rescue Exception => e
puts "Interrupt a traversé le rescue nu : #{e.class}"
endLe rescue nu n’a rien pris : seul le rescue Exception du dessus a vu passer Interrupt.
Rattraper large ou précis
begin
raise PaiementRefuse, "carte"
rescue AppError => e
puts "#{e.class} : #{e.message}" # => PaiementRefuse : carte
endCe que l’image cache
La frontière protège, mais ne dispense pas de choisir la bonne classe : un rescue StandardError trop large peut encore cacher un bug. Rattrape ce que tu sais traiter, laisse monter le reste.
Pour aller plus loin
Le numéro ci-dessous détaille la hiérarchie des exceptions et le piège du rescue fourre-tout, avec des exercices.
The proof, with Ruby 3.4
class AppError < StandardError; end
class PaiementRefuse < AppError; end
p PaiementRefuse.ancestors.first(5)
# => [PaiementRefuse, AppError, StandardError, Exception, Object]
p Interrupt.superclass # => SignalException
p SignalException.superclass # => ExceptionA bare rescue lets Ctrl-C through
begin
begin; raise Interrupt; rescue => e; puts "rescue nu a pris"; end
rescue Exception => e
puts "Interrupt a traversé le rescue nu : #{e.class}"
endThe bare rescue caught nothing: only the outer rescue Exception saw Interrupt go by.
Rescuing broadly or precisely
begin
raise PaiementRefuse, "carte"
rescue AppError => e
puts "#{e.class} : #{e.message}" # => PaiementRefuse : carte
endWhat the picture hides
The boundary protects you, but doesn’t excuse choosing the right class: an overly broad rescue StandardError can still hide a bug. Rescue what you know how to handle, let the rest bubble up.
Go deeper
The issue below details the exception hierarchy and the catch-all rescue trap, with exercises.