-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exception.rb
61 lines (47 loc) · 1.11 KB
/
Exception.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def inverse(x)
raise ArgumentError, 'Argument is not a numeric! Please, input a again!' unless x.is_a? Numeric
1.0/x
end
puts inverse(2)
# puts inverse('Hello')
# program will terminate at which the exception is caused if exception is not rescued
def raise_and_rescue
begin
puts 'begin'
inverse('Hello')
puts 'end begin'
rescue Exception => e
puts e
puts e.message
puts e.backtrace.inspect
end
end
raise_and_rescue
puts 'program is terminated'
class Name
attr_reader :first_name, :last_name
def initialize(first_name, last_name)
self.first_name = first_name
self.last_name = last_name
end
def name
"#{@first_name} #{@last_name}"
end
private
def first_name=(first_name)
if first_name == nil or first_name.size == 0
raise ArgumentError.new('Everyone must have a first_name')
end
@first_name = first_name
end
def last_name=(last_name)
if last_name == nil or last_name.size == 0
raise ArgumentError.new('Everyone must have a last_name')
end
@last_name = last_name
end
end
person = Name.new('luong', 'nguyen')
puts person.name
person = Name.new('luong', nil)
puts person.name