From 5a2995ec8364108f82dc2d6b40e33905358e645f Mon Sep 17 00:00:00 2001 From: Bozhidar Batsov Date: Fri, 30 Oct 2026 15:40:05 +0200 Subject: [PATCH] Add a guideline against raising Exception Lint/RaiseException links to #raise-exception, which didn't exist yet. #775 proposed one, but its good example raised StandardError, which is a base class rather than something to raise. A specific exception class or a plain RuntimeError is what should replace Exception. --- README.adoc | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.adoc b/README.adoc index d97c245a..f8082d02 100644 --- a/README.adoc +++ b/README.adoc @@ -2054,6 +2054,24 @@ raise RuntimeError, 'message' raise 'message' ---- +=== Raising `Exception` [[raise-exception]] + +Don't raise `Exception`. +A plain `rescue` only rescues `StandardError`, so catching it takes a <> of `Exception`, which traps signals and calls to `exit` as well. +Raise a more specific exception class instead, or just pass a message to get a `RuntimeError`. + +[source,ruby] +---- +# bad +raise Exception, 'message' + +# good +raise ArgumentError, 'message' + +# good - signals a RuntimeError by default +raise 'message' +---- + === Exception Class Messages [[exception-class-messages]] Prefer supplying an exception class and a message as two separate arguments to `raise`, instead of an exception instance.