Created
April 21, 2016 18:19
-
-
Save bparanj/2c6ef35c609a6465c641bebdb5a78e4d to your computer and use it in GitHub Desktop.
How to separate business logic from exception handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def run_with_exception_handling | |
begin | |
puts '1' | |
yield | |
puts '2' | |
rescue Exception | |
puts 'Exception thrown' | |
end | |
end | |
def test_runner | |
run_with_exception_handling do | |
raise 'boom' | |
end | |
end | |
test_runner |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any method can take a block in Ruby. There is no syntax required in the method defined to accept a block. The
test_runner
method can call therun_with_exception_handling
and focus only on the business logic. The exception handling method can catch and handle specific exceptions that are recoverable by retries, non-recoverable and user errors separately.