Skip to content

Instantly share code, notes, and snippets.

@bparanj
Created April 21, 2016 18:19
Show Gist options
  • Save bparanj/2c6ef35c609a6465c641bebdb5a78e4d to your computer and use it in GitHub Desktop.
Save bparanj/2c6ef35c609a6465c641bebdb5a78e4d to your computer and use it in GitHub Desktop.
How to separate business logic from exception handling
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
@bparanj
Copy link
Author

bparanj commented Apr 21, 2016

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 the run_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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment