File: C:/Ruby27-x64/share/ri/2.7.0/system/syntax/page-exceptions_rdoc.ri
U:RDoc::TopLevel[ i I"syntax/exceptions.rdoc:ETcRDoc::Parser::Simpleo:RDoc::Markup::Document:@parts[(S:RDoc::Markup::Heading:
leveli: textI"Exception Handling;To:RDoc::Markup::BlankLine o:RDoc::Markup::Paragraph;[I"5Exceptions are rescued in a +begin+/+end+ block:;T@
o:RDoc::Markup::Verbatim;[
I"begin
;TI" # code that might raise
;TI"rescue
;TI" # handle exception
;TI" end
;T:@format0o;
;[I"TIf you are inside a method, you do not need to use +begin+ or +end+ unless you ;TI"3wish to limit the scope of rescued exceptions:;T@
o;;[
I"def my_method
;TI"
# ...
;TI"rescue
;TI"
# ...
;TI" end
;T;0o;
;[I";The same is true for a +class+, +module+, and +block+:;T@
o;;[I"[0, 1, 2].map do |i|
;TI" 10 / i
;TI"rescue ZeroDivisionError
;TI" nil
;TI" end
;TI"#=> [nil, 10, 5]
;T;0o;
;[I"FYou can assign the exception to a local variable by using <tt>=> ;TI"8variable_name</tt> at the end of the +rescue+ line:;T@
o;;[I"begin
;TI"
# ...
;TI"rescue => exception
;TI" warn exception.message
;TI". raise # re-raise the current exception
;TI" end
;T;0o;
;[I"QBy default, StandardError and its subclasses are rescued. You can rescue a ;TI"Tspecific set of exception classes (and their subclasses) by listing them after ;TI"+rescue+:;T@
o;;[
I"begin
;TI"
# ...
;TI"%rescue ArgumentError, NameError
;TI"+ # handle ArgumentError or NameError
;TI" end
;T;0o;
;[I"DYou may rescue different types of exceptions in different ways:;T@
o;;[I"begin
;TI"
# ...
;TI"rescue ArgumentError
;TI" # handle ArgumentError
;TI"rescue NameError
;TI" # handle NameError
;TI"rescue
;TI"" # handle any StandardError
;TI" end
;T;0o;
;[I"UThe exception is matched to the rescue section starting at the top, and matches ;TI"Tonly once. If an ArgumentError is raised in the begin section, it will not be ;TI"*handled in the StandardError section.;T@
o;
;[I"&You may retry rescued exceptions:;T@
o;;[I"begin
;TI"
# ...
;TI"rescue
;TI"D # do something that may change the result of the begin block
;TI"
retry
;TI" end
;T;0o;
;[I"QExecution will resume at the start of the begin block, so be careful not to ;TI"create an infinite loop.;T@
o;
;[ I"RInside a rescue block is the only valid location for +retry+, all other uses ;TI"Swill raise a SyntaxError. If you wish to retry a block iteration use +redo+. ;TI"MSee {Control Expressions}[rdoc-ref:syntax/control_expressions.rdoc] for ;TI"
details.;T@
o;
;[I"RTo always run some code whether an exception was raised or not, use +ensure+:;T@
o;;[I"begin
;TI"
# ...
;TI"rescue
;TI"
# ...
;TI"ensure
;TI" # this always runs
;TI" end
;T;0o;
;[I"@You may also run some code when an exception is not raised:;T@
o;;[I"begin
;TI"
# ...
;TI"rescue
;TI"
# ...
;TI"
else
;TI"5 # this runs only when no exception was raised
;TI"ensure
;TI"
# ...
;TI"end;T;0:
@file@:0@omit_headings_from_table_of_contents_below0