Ruby
-
Handling / Raising ExceptionsRuby 2022. 9. 11. 05:12
def format_name(first, last) if !(first.instance_of?(String) && last.instance_of?(String)) raise "arguments must be strings" end first.capitalize + " " + last.capitalize end first_name = 42 last_name = true begin puts format_name(first_name, last_name) rescue # do stuff to rescue the "arguments must be strings" exception... puts "there was an exception :(" end there was an exception :( raise는 예외..
-
Raising ExceptionsRuby 2022. 9. 11. 05:02
# Raising Exceptions # def format_name(first, last) if !(first.instance_of?(String) && last.instance_of?(String)) raise "arguments must be strings" # custom error message 를 만들고 에러가 뜬 순간 method는 종료된다. raise: this is how we can make exceptions manually. end first.capitalize + " " + last.capitalize end p format_name("grace", "hopper") p format_name(42, true) raise를 사용하여 오류(예외)를 어떻게 보여주는지 설정 할 수 있다.
-
Handling ExceptionsRuby 2022. 9. 11. 03:55
# handling Exceptions # num = 0 begin # error 가 뜨는 구간이 지정하고 exception을 만나기까지 실행 > exception에 도달되면 즉시 jump to rescue 된다. puts "dividing 10 by #{num}" ans = 10 / num puts "the answer is #{ans}" rescue # error가 떳을 때 대처되는 handling 방식 puts "There was an error with that division." end puts "--------" puts "finished" dividing 10 by 0 There was an error with that division. -------- finished num = 5 begi..