2016年9月10日土曜日

開発環境

Head First Ruby (Jay McGavren (著)、O'Reilly Media)のChapter 12.(Exceptions: Handling the Unexpected)、Trying again after an error with "retry"、EXERCISE(No. 6431))を取り組んでみる。

EXERCISE(No. 6431))

コード(Emacs)

#!/usr/bin/env ruby2.3
# -*- coding: utf-8 -*-

class TestScoreError < StandardError
end

score = 52
begin
  if score > 60
    puts "passing grade"
  else
    raise TestScoreError, "failing grade"
  end
rescue TestScoreError => error
  puts "Received #{error.message}. Taking make-up exam..."
  score = 63
  retry
end

入出力結果(Terminal)

$ ./sample2.rb
Received failing grade. Taking make-up exam...
passing grade
$

0 コメント:

コメントを投稿

Comments on Google+: