ruby exception handling

About this article

In job change activities, I sometimes described exception handling in technical issues, but I have never experienced it before, so it is an output

What is exception handling?

begin

#Specify code to detect problems when running a program
#Write code that is likely to cause an error

rescue

#Describe how to respond when a problem is detected

end

Raise an exception

With the following code, 100 / 0 cannot be executed, so processing will stop at ʻanswer = 100 / number. ZeroDivisionError` is the corresponding error, and describe the processing you want to perform when this exception occurs.

number = 0
answer = 20 / num
puts answer
puts 2

=>`/': divided by 0 (ZeroDivisionError)
	from Main.rb:4:in `<main>'

Exception handling

If you write as follows, the exception will normally occur at 100/0, so the processing will stop, but since the processing in rescue is performed, the processing will be executed to the end.

puts 1
begin
    #Write the code you want to detect
    number = 0
    answer = 20 / num
    puts answer
rescue ZeroDivisionError => e
   #Describe the process you want to perform when ZeroDivisionError occurs (register an exception handler)
    p e
ensure
    puts 2
end


=>
1
#<ZeroDivisionError: divided by 0>
2

What is e?

It is a ZeroDivisionError object (variable) that stores the details of the exception that occurred. In other words, if you output this, you can understand why the error occurred.

How to output

Objects have various messages and can retrieve necessary information as shown below.

p e

puts e.message

p e.backtrace


=>
#<ZeroDivisionError: divided by 0>

divided by 0

["Main.rb:4:in `/'", "Main.rb:4:in `<main>'"]


Recommended Posts

ruby exception handling
Ruby exception handling
About Ruby exception handling
[Ruby] Exception handling basics
Exception handling
Exception handling Exception
[Ruby] Exception handling in functions
Java exception handling?
About exception handling
[Java] Exception handling
☾ Java / Exception handling
Java exception handling
Java exception handling
[Ruby] Exception handling, give back if given
Exception handling practice (ArithmeticException)
Spring Boot exception handling
Ruby Learning # 28 Handling Errors
[Java] Practice of exception handling [Exception]
[Java] About try-catch exception handling
exception
Java exception handling usage rules
Exception handling techniques in Java
[In-house study session] Java exception handling (2017/04/26)
[Rails] How to write exception handling?
Exception handling with a fluid interface
Step-by-step understanding of Java exception handling
[For Java beginners] About exception handling
Solving with Ruby AtCoder 1st Algorithm Practical Test A Exception Handling
[Ruby] Array
Ruby basics
Ruby learning 5
Ruby basics
Ruby Review 2
Ruby addition
Refactoring Ruby
Ruby learning 3
Ruby setting 2
Ruby problem ⑦
Self-made Exception
Ruby learning 2
Java (exception handling, threading, collection, file IO)
[Ruby] Block
Refactoring Ruby
ruby calculator
Tips for gRPC error handling in Ruby
Ruby learning 6
Ruby settings 1
Refactoring Ruby
Ruby basics
Ruby memo
Ruby learning 1
Ruby Review 1
[Ruby] Module
try-catch-finally exception handling How to use java
I want to design a structured exception handling
Questions in java exception handling throw and try-catch