るりまサーチ

最速Rubyリファレンスマニュアル検索!
225件ヒット [1-100件を表示] (0.011秒)
トップページ > クラス:Exception[x]

ライブラリ

キーワード

検索結果

<< 1 2 3 > >>

Exception#==(other) -> bool (1.0)

{: since=""}

...した場合は
[m:Exception#exception] を実行して変換を試みます。

```ruby title="例"
require "date"
def check_long_month(month)
return if Date.new(2000, month, -1).day == 31
raise "#{month} is not long month"
end

def get_exception
return begin
yield
rescue =...
...> e
e
end
end

results = [2, 2, 4].map { |e | get_exception { check_long_month(e) } }
p results.map { |e| e.class }
# => [RuntimeError, RuntimeError, RuntimeError]
p results.map { |e| e.message }
# => ["2 is not long month", "2 is not long month", "4 is not long month"]

# class, message, back...

Exception#as_json(*args) -> Hash (1.0)

{: since="1.9.3"}

...ince="1.9.3"}

`self` を JSON 形式の文字列に変換する際に使う、中間表現となるハッシュに変換して返します。
[m:Exception#to_json] が内部で使用しています。

ハッシュには、[m:JSON.create_id] をキーとしてクラス名が、`'m'` にメッセー...
...- **param** `args` -- 無視されます。

```ruby title="例"
require 'json/add/exception'

hash = Exception.new('Foo').as_json
p hash['json_class'] # => "Exception"
p hash['m'] # => "Foo"
p hash['b'] # => nil
```

- **SEE** [m:Exception#to_json], [m:Exception.json_create]...

Exception#backtrace -> [String] (1.0)

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

...urcefile}:#{sourceline}"
(トップレベルの場合)

という形式の [c:String] の配列です。

```ruby title="例"
def methd
raise
end

begin
methd
rescue => e
p e.backtrace
end

#=> ["filename.rb:2:in `methd'", "filename.rb:6"]
```

- **SEE** [m:Exception#backtrace_locations]...
...e}:#{sourceline}"
(トップレベルの場合)

という形式の [c:String] の配列です。

```ruby title="例"
def methd
raise
end

begin
methd
rescue => e
p e.backtrace
end

#=> ["filename.rb:2:in 'Object#methd'", "filename.rb:6"]
```

- **SEE** [m:Exception#backtrace_locations]...

Exception#backtrace_locations -> [Thread::Backtrace::Location] (1.0)

バックトレース情報を返します。[m:Exception#backtrace]に似ていますが、 [c:Thread::Backtrace::Location] の配列を返す点が異なります。

...バックトレース情報を返します。[m:Exception#backtrace]に似ていますが、
[c:Thread::Backtrace::Location] の配列を返す点が異なります。

現状では [m:Exception#set_backtrace] によって戻り値が変化する事はありません。

```ruby title="例: test.rb...
...get_exception
return begin
yield
rescue => e
e
end
end

e = get_exception { check_long_month(2) }
p e.backtrace_locations
# => ["test.rb:4:in `check_long_month'", "test.rb:15:in `block in <main>'", "test.rb:9:in `get_exception'", "test.rb:15:in `<main>'"]
```

- **SEE** [m:Exception#ba...
...get_exception
return begin
yield
rescue => e
e
end
end

e = get_exception { check_long_month(2) }
p e.backtrace_locations
# => ["test.rb:4:in 'check_long_month'", "test.rb:15:in 'block in <main>'", "test.rb:9:in 'get_exception'", "test.rb:15:in '<main>'"]
```

- **SEE** [m:Exception#ba...

Exception#cause -> Exception | nil (1.0)

self の前の例外(self が rescue 節や ensure 節の中で発生した例外の場合、その前に発生していた元々の例外)を返します。存在しない場合は nil を返します。

self の前の例外(self が rescue 節や ensure 節の中で発生した例外の場合、その前に発生していた元々の例外)を返します。存在しない場合は nil を返します。

```ruby title="例"
begin
begin
raise "inner"
rescue
raise "outer"
end
rescue
p $! # => #<RuntimeError: outer>
p $!.cause # => #<RuntimeError: inner>
end
```

絞り込み条件を変える

Exception#detailed_message(highlight: false, **opt) -> String (1.0)

例外のメッセージに情報を追加した文字列を返します。

...加した文字列を返します。

[m:Exception#message] との違いは、1 行目に例外クラス名が付くことです。
highlight に true を指定すると、エスケープシーケンスによる文字装飾も付きます。

[m:Exception#full_message] と違い、highlight の既...
...可能性があります。

```ruby
class MyError < StandardError
def detailed_message(highlight: false, **opt)
"custom: #{message}"
end
end

begin
raise MyError, "x"
rescue => e
p e.detailed_message # => "custom: x"
end
```

- **SEE** [m:Exception#message], [m:Exception#full_message]...

Exception#exception -> self (1.0)

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

...返します。引数を指定した場合 自身のコピーを生成し [m:Exception#message] 属性を error_message にして返します。

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

- **param** `error_message` -- エラ...
...ーメッセージを表す文字列を指定します。

```ruby title="例"
begin
# ... # 何か処理
rescue => e
raise e.exception("an error occurs during hogehoge process") # 詳しいエラーメッセージ
end
```...

Exception#exception(error_message) -> Exception (1.0)

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

...返します。引数を指定した場合 自身のコピーを生成し [m:Exception#message] 属性を error_message にして返します。

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

- **param** `error_message` -- エラ...
...ーメッセージを表す文字列を指定します。

```ruby title="例"
begin
# ... # 何か処理
rescue => e
raise e.exception("an error occurs during hogehoge process") # 詳しいエラーメッセージ
end
```...

Exception#full_message(highlight: true, order: :bottom) -> String (1.0)

例外の整形された文字列を返します。

...ht` -- エスケープシーケンスによる文字装飾をつけるかどうかを指定します。
デフォルト値は [m:Exception.to_tty?] の返り値と同じです。

- **param** `order` -- :top か :bottom で指定する必要があります。
バック...
...m"
$stderr = $stdout
p e.full_message # => "test.rb:2:in `<main>': test (RuntimeError)\n"
$stderr = STDERR
p e.full_message # => "\e[1mTraceback \e[m(most recent call last):\ntest.rb:2:in `<main>': \e[1mtest (\e[4;1mRuntimeError\e[m\e[1m)\n\e[m"
end
```

- **SEE** [m:Exception.to_tty?]...
...m"
$stderr = $stdout
p e.full_message # => "test.rb:2:in '<main>': test (RuntimeError)\n"
$stderr = STDERR
p e.full_message # => "\e[1mTraceback \e[m(most recent call last):\ntest.rb:2:in '<main>': \e[1mtest (\e[4;1mRuntimeError\e[m\e[1m)\n\e[m"
end
```

- **SEE** [m:Exception.to_tty?]...
<< 1 2 3 > >>