2017年7月26日 星期三
Ruby 可以在 class 內定義 exception method,這樣就可以 raise 這個 class
不囉唆,上 code
Raising Non-Exceptions
If we provide an exception method, any object can be raised as an exception.
Imagine you have a class called HTMLSafeString which contains a string of text. You might want to make it possible to pass one of these “safe” strings to raise just like a normal string. To do this we simply add an exception method that creates a new RuntimeError.
class HTMLSafeString
def initialize(value)
@value = value
end
def exception
RuntimeError.new(@value)
end
end
raise HTMLSafeString.new("helloworld")”
Excerpt From: Starr Horne. “Mastering Ruby Exceptions.” iBooks.
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言