-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_throw_catch.rb
39 lines (35 loc) · 1.02 KB
/
test_throw_catch.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def promptAndGet(prompt)
print prompt
res = readline.chomp
throw :quitRequested if res == "!"
return res
end
# catch :quitRequested do
# name = promptAndGet("Name: ")
# age = promptAndGet("Age: ")
# sex = promptAndGet("Sex: ")
# end
# -------------------------
class FileSaveError < StandardError
attr_reader :reason
def initialize(reason)
@reason = reason
puts "Inside the constructor" # 5 => Inside the constructor
end
end
File.open("modules/data/nothing.txt", "w") do |file|
begin
file.puts "some string" # 1
raise "Oups" # 2
rescue
# Something went wrong!
puts "rescue part bagin" # 3 => rescue part bagin
puts $! # 4 => Oups
raise FileSaveError.new($!)
puts "rescue part end"
ensure
puts "ensure part bagin" # 6 => ensure part bagin
file.close # 7
puts "ensure part end" # 8 => ensure part end
end
end # 9