class Encoding::UndefinedConversionError + EncodingError + StandardError + Exception
クラスの継承リスト: Encoding::UndefinedConversionError < EncodingError < StandardError < Exception < Object < Kernel < BasicObject
要約
エンコーディング変換後の文字が存在しない場合に発生する例外。
UTF-8 にしかない文字を EUC-JP に変換しようとした場合などに発生します。
"\u2603".encode(Encoding::EUC_JP) #=> Encoding::UndefinedConversionError: U+2603 from UTF-8 to EUC-JP
変換が多段階でなされ、その途中で例外が生じた場合は、 例外オブジェクトが保持するエラー情報はその中間のものになります。
ec = Encoding::Converter.new("ISO-8859-1", "EUC-JP") # ISO-8859-1 -> UTF-8 -> EUC-JP begin ec.convert("\xa0") # NO-BREAK SPACE, which is available in UTF-8 but not in EUC-JP. rescue Encoding::UndefinedConversionError p $!.source_encoding #=> #<Encoding:UTF-8> p $!.destination_encoding #=> #<Encoding:EUC-JP> p $!.source_encoding_name #=> "UTF-8" p $!.destination_encoding_name #=> "EUC-JP" puts $!.error_char.dump #=> "\u{a0}" p $!.error_char.encoding #=> #<Encoding:UTF-8> 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"]
destination_encoding -> Encoding
-
エラーを発生させた変換の変換先のエンコーディングを Encoding オブジェクトで返します。
[SEE_ALSO] Encoding::UndefinedConversionError#source_encoding
destination_encoding_name -> String
-
エラーを発生させた変換の変換先のエンコーディングを文字列で返します。
[SEE_ALSO] Encoding::UndefinedConversionError#destination_encoding
error_char -> String
-
エラーを発生させた1文字を文字列で返します。
ec = Encoding::Converter.new("UTF-8", "EUC-JP") begin ec.convert("\u{a0}") rescue Encoding::UndefinedConversionError puts $!.error_char.dump #=> "\u{a0}" end
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 を設定し、設定されたバックトレース 情報を返します。
source_encoding -> Encoding
-
エラーを発生させた変換の変換元のエンコーディングを Encoding オブジェクトで返します。
変換が多段階になされる場合は元の文字列のものではない エンコーディングが返される場合があることに注意してください。
[SEE_ALSO] Encoding::UndefinedConversionError#destination_encoding
source_encoding_name -> Encoding
-
エラーを発生させた変換の変換元のエンコーディングを文字列で返します。
[SEE_ALSO] Encoding::UndefinedConversionError#source_encoding