> For the complete documentation index, see [llms.txt](https://kelby.gitbook.io/rails-beginner-s-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kelby.gitbook.io/rails-beginner-s-guide/activesupport_other_class_and_module/activesupport_rescuable.md).

# Rescuable

类方法：

`rescue_from(*klasses, &block)`

klasses 表示一个或多个异常类。如果有 :with 选项，则用其 value(一般是个方法) 处理；否则，需要传递 block 来处理。

```ruby
class ApplicationController < ActionController::Base
  # 一个或多个异常类，有 :with 选项
  rescue_from User::NotAuthorized, with: :deny_access # 自定义的异常处理方法
  rescue_from ActiveRecord::RecordInvalid, with: :show_errors

  # 一个或多个异常类，传递 block
  rescue_from 'MyAppError::Base' do |exception|
    render xml: exception, status: 500
  end

  protected
    def deny_access
      # ...
    end

    def show_errors(exception)
      exception.record.new_record? ? ...
    end
end
```

实现：类似'实例变量的运用'，`rescue_from` 把希望捕捉的异常类放到一个变量(rescue\_handlers)里。抛出异常时，会对异常进行检查(rescue\_with\_handler)，如果抛出的异常恰好被包含在这个变量(rescue\_handlers)，则用我们的方式进行处理。

实例方法：

```
handler_for_rescue

rescue_with_handler
```
