[Rails] How to write exception handling?

What is exception handling?

Exception handling is to handle when an error occurs. It is convenient to write exception handling to identify the cause of the error and prevent problems on the system side.

How to write exception handling

Basic form of begin, rescue

Enclose the part that is likely to be the target of the error with begin, and write the processing when the error occurs in rescue.

  100 / 0
  p "Does not break at 0"

puts "Good morning"

Write with one liner

It is also possible to write with one liner.

sample_1 = 10 / 0 rescue 0

sample_2 = 10 / nil rescue 0

puts sample_1 #=>0
puts sample_2 #=>0

Get the error content (e)

You can also specify an argument after rescue to store the error content in a variable.

  10 / 0
rescue => e
  puts e #=> divided by 0

Conditional branch for each error

Alternatively, by specifying an error message after rescue, You can make a conditional branch to decide which error to take and what to do.

  10 / 0
rescue NoMethodError
  puts "There is no method"
rescue ZeroDivisionError
  puts "Does not break at 0"

How to use raise

It is used when you want to explicitly generate an error and interrupt the process when the parameters are not expected or when an unauthorized access occurs.

  raise NoMethodError #The class of exception you want to raise
rescue => e
  puts e

It is also possible to output an error message.

  raise RuntimeError, "Run-time error"
rescue => e
  puts e

How to use retry

If you get an error, you can go back to begin and run it again.

num = 0
  puts 10 / num
rescue ZeroDivisionError => e
  puts e
  num = 1
  retry #Run the begin block again

puts "Finished"

How to use ensure

You can write a process that will be executed with or without an exception.

  puts "No exception"
rescue => e
  puts e
  puts "Absolutely run here!"

Recommended Posts

[Rails] How to write exception handling?
How to write Rails
How to write Rails validation
How to write Rails seed
How to write Rails routing
[Rails] How to write in Japanese
Rails on Tiles (how to write)
try-catch-finally exception handling How to use java
How to uninstall Rails
How to write docker-compose
How to write Mockito
How to write migrationfile
Rails: How to write a rake task nicely
[Rails] How to write when making a subquery
[rails] How to post images
How to write good code
[Rails] How to use enum
Bit Tetris (how to write)
[Rails] How to install devise
[Rails] How to use enum
How to write java comments
How to read rails routes
[Refactoring] How to write routing
Great poor (how to write)
[Note] How to write Dockerfile/docker-compose.yml
How to use rails join
How to write Junit 5 organized
How to terminate rails server
[Rails] How to use validation
[Ruby] How to write blocks
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to implement scraping
[Rails] How to make seed
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
[Rails] How to use Scope
How to write a date comparison search in Rails
[Rails] How to use gem "devise"
How to deploy jQuery on Rails
[Rails] How to install Font Awesome
[Rails] How to use devise (Note)
[Rails] Two ways to write form_with
[Rails] How to use flash messages
[rails] How to display db information
Studying Java # 6 (How to write blocks)
How to write a migration from Rails datetime type to date type
[Rails] How to prevent screen transition
How to use Ruby on Rails
How to deploy Bootstrap on Rails
[Rails] How to speed up docker-compose
Baseball ball count (how to write)
[Rails] How to add new pages
How to write a ternary operator
Exception handling
[Rails] How to install ImageMagick (RMajick)
How to write Java variable declaration
[Rails] How to install Font Awesome
[Rails] How to use Active Storage
How to introduce jQuery in Rails 6