Ruby 1.9.3 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > Interruptクラス

class Interrupt + SignalException + Exception

クラスの継承リスト: Interrupt < SignalException < Exception < Object < Kernel < BasicObject

要約

SIGINT シグナルを捕捉していないときに SIGINT シグナルを受け取ると発生します。 SIGINT 以外のシグナルを受信したときに発生する例外については SignalException を参照してください。

使用例

=begin
#SIGINTを補足したい場合
Signal.trap('INT'){
  print "\nINTを補足した。\n"
  exit 1
}
=end

begin
  begin
    print "z"
    $stdout.flush
    sleep(1)
  end while true
rescue Interrupt
  print "!!\n"
  exit 1
end

特異メソッド

new(error_message = nil) -> Exception
exception(error_message = nil) -> Exception

例外オブジェクトを生成して返します。

[PARAM] error_message:
エラーメッセージを表す文字列を指定します。このメッセージは 属性 Exception#message の値になり、デフォルトの例外ハンドラで表示されます。

インスタンスメソッド

self == other -> bool

自身と指定された other のクラスが同じであり、message と backtrace が == メソッドで比較して 等しい場合に true を返します。そうでない場合に false を返します。

[PARAM] other:
自身と比較したいオブジェクトを指定します。
backtrace -> [String]

バックトレース情報を返します。

デフォルトでは

  • "#{sourcefile}:#{sourceline}:in `#{method}'" (メソッド内の場合)
  • "#{sourcefile}:#{sourceline}" (トップレベルの場合)

という形式の String の配列です。

def methd
  raise
end

begin
  methd
rescue => e
  p e.backtrace
end

#=> ["filename.rb:2:in `methd'", "filename.rb:6"]
exception -> self
exception(error_message) -> Exception

引数を指定しない場合は self を返します。引数を指定した場合 自身のコピー を生成し Exception#message 属性を error_message にして返します。

Kernel.#raise は、実質的に、例外オブジェクトの exception メソッドの呼び出しです。

[PARAM] error_message:
エラーメッセージを表す文字列を指定します。
begin
 ...        # 何か処理
rescue => e
 raise e.exception("an error occurs during hogehoge process")  # 詳しいエラーメッセージ
end
message -> String
to_s -> String

エラーメッセージをあらわす文字列を返します。

begin
  1 + nil
rescue => e
  p e.message   #=>  "nil can't be coerced into Fixnum"
end
set_backtrace(errinfo) -> nil | String | [String]

バックトレース情報に errinfo を設定し、設定されたバックトレース 情報を返します。

[PARAM] errinfo:
nil、String あるいは String の配列のいずれかを指定します。
class Interrupt