[RAILS] [Ruby] Exception handling in functions

Introduction

This article is a supplement to a number of exception handling. Specifically, I will leave the result of verifying the pattern not introduced in the article.

environment

Use in functions

In the function, you can write as follows.

def method
  puts "hoge"
  a = 1 / 0
rescue ZeroDivisionError
  puts $!
end

It is a form that omits begin and end. In the case of such a shape

a = 1 / 0

Is obvious that will cause an error. But what if:

def method
  #Huge processing 1
  #Processing that is likely to cause an error
  #Huge processing 2
rescue StandardError
  puts $!
end

By the way, it is doubtful that the person who sees this code can immediately see the processing that is likely to cause an error. In that case, I think you should dare to write begin and end.

def method
  #Huge processing 1
  begin
    #Processing that is likely to cause an error
  rescue StandardError
    puts $!
  end
  #Huge processing 2
end

This way you can see where the error is likely to occur and you don't have to scroll a lot to see the rescue. By the way, if you return with rescue, the huge amount of process 2 will not be processed, so if you definitely want it to be processed, put it in ensure.

def method
  #Huge processing 1
  begin
    #Processing that is likely to cause an error
  rescue StandardError
    puts $!
    return
  ensure
    #Huge processing 2(Even if there is a return in rescue, it will be processed)
  end
end

If there are multiple processes that are likely to cause an error, I think it is simple to write them together at the end of the function. Writing begin, rescue, end every time will make it less readable. Everything is right.

def method
  #Processing that is likely to cause various errors

  #Other processing

rescue ZeroDivisionError, ArgumentError
  puts $!
rescue StandardError
  puts $!
  puts $@
end

By the way, if you write StandardError first, this guy will do most of the error handling. So let's start with the child class.

Recommended Posts

[Ruby] Exception handling in functions
ruby exception handling
Ruby exception handling
About Ruby exception handling
[Ruby] Exception handling basics
Exception handling techniques in Java
Exception handling
Exception handling Exception
About exception handling
About exception handling
[Ruby] Exception handling, give back if given
Class in Ruby
[Java] Exception handling
Heavy in Ruby! ??
Tips for gRPC error handling in Ruby
☾ Java / Exception handling
Java exception handling
Java exception handling
Questions in java exception handling throw and try-catch
Exception handling practice (ArithmeticException)
About eval in Ruby
Azure functions in java
Spring Boot exception handling
Output triangle in Ruby
Error handling in Graphql-ruby
Ruby Learning # 28 Handling Errors
Variable type in ruby
Fast popcount in Ruby
Handling of date and time in Ruby. Use Date and Time properly.
ABC177 --solving E in Ruby
Classes that require exception handling
Validate JWT token in Ruby
Implemented XPath 1.0 parser in Ruby
Java's first exception handling (memories)
Read design patterns in Ruby
[Java] Practice of exception handling [Exception]
Write class inheritance in Ruby
[Java] About try-catch exception handling
Integer unified into Integer in Ruby 2.4
Use ruby variables in javascript.
Java exception handling usage rules
Multiplication in a Ruby array
About regular expressions in Ruby
Create Azure Functions in Java
Birthday attack calculation in Ruby
Ruby study notes (variables & functions)
Judgment of fractions in Ruby
Find Roman numerals in Ruby
Try using gRPC in Ruby
Ruby variables and functions (methods)
[Ruby] Find numbers in arrays
Handling of line beginning and line ending in regular expressions in Ruby
NCk mod p in Ruby
Chinese Remainder Theorem in Ruby
Solving with Ruby AtCoder 1st Algorithm Practical Test A Exception Handling
Leverage Either for individual exception handling in the Java Stream API
Sorting hashes in a Ruby array
Basics of sending Gmail in Ruby
How to iterate infinitely in Ruby
[In-house study session] Java exception handling (2017/04/26)
Achieve 3-digit delimited display in Ruby