begin-rescue
(try-catch
).
# basic syntax begin # ... rescue => e # ... end # rescuing specific Error class name begin # ... rescue ArgumentError => e # ... end
Full Chain
begin # may raise an exception rescue AnException # exception handler rescue AnotherException # exception handler else # other exceptions ensure # always executed end
Exceptions shouldn't be used for validations.
You shouldn't traverse the stack for validations in general.
What you should NOT be doing
What you should be doing instead
if
/else
on the validity, rather than rescue
In Ruby, rescuing multiple exceptions are easy.
begin ... rescue Exception1, Exception2 ... rescue Exception1 ... rescue Exception2 ... end