Rubyにおける例外処理

  • Rubyにおける例外処理がまたよく分からなくなったのでそのメモ
    • 下記のプログラムにおいてShiftingErrorのrescueは必要なのかが分からない
    require 'logger'
    require 'thread'

    class TestException < StandardError; end

    log = Logger.new('looplog.log', 'daily')
    loop do
      th = Thread.new {
        begin
          loop do
            log.debug('debug')
          end
        rescue TestException
          puts $!
        rescue Logger::ShiftingError
          puts $!
        end
      }
      sleep 1
      th.raise TestException
    end
    • このrescueがないと数回に一回ShiftingErrorで落ちる模様
  • logger中でShiftingErrorがraiseされる該当コードは以下
        def write(message)
          @mutex.synchronize do
            if @shift_age and @dev.respond_to?(:stat)
              begin
                check_shift_log
              rescue
                raise Logger::ShiftingError.new("Shifting failed. #{$!}")
              end
            end
            @dev.write(message)
          end
        end