D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
opt
/
alt
/
ruby34
/
share
/
ri
/
system
/
Exception
/
Filename :
set_backtrace-i.ri
back
Copy
U:RDoc::AnyMethod[iI"set_backtrace:ETI"Exception#set_backtrace;TF:publico:RDoc::Markup::Document:@parts[o:RDoc::Markup::Paragraph; [I"DSets the backtrace value for +self+; returns the given +value+.;To:RDoc::Markup::BlankLine o; ; [I"The +value+ might be:;T@o:RDoc::Markup::List: @type:BULLET:@items[ o:RDoc::Markup::ListItem:@label0; [o; ; [I"-an array of Thread::Backtrace::Location;;To;;0; [o; ; [I""an array of String instances;;To;;0; [o; ; [I"!a single String instance; or;To;;0; [o; ; [I"+nil+.;T@o; ; [ I"GUsing array of Thread::Backtrace::Location is the most consistent ;TI"Loption: it sets both #backtrace and #backtrace_locations. It should be ;TI"Npreferred when possible. The suitable array of locations can be obtained ;TI"Mfrom Kernel#caller_locations, copied from another error, or just set to ;TI"Ethe adjusted result of the current error's #backtrace_locations:;T@o:RDoc::Markup::Verbatim; [I"require 'json' ;TI" ;TI"def parse_payload(text) ;TI"+ JSON.parse(text) # test.rb, line 4 ;TI"$rescue JSON::ParserError => ex ;TI"6 ex.set_backtrace(ex.backtrace_locations[2...]) ;TI" raise ;TI" end ;TI" ;TI"&parse_payload('{"wrong: "json"') ;TI"f# test.rb:4:in 'Object#parse_payload': unexpected token at '{"wrong: "json"' (JSON::ParserError) ;TI"# ;TI"<# An error points to the body of parse_payload method, ;TI"B# hiding the parts of the backtrace related to the internals ;TI"# of the "json" library ;TI" ;TI"A# The error has both #backtace and #backtrace_locations set ;TI"# consistently: ;TI"begin ;TI"( parse_payload('{"wrong: "json"') ;TI"rescue => ex ;TI" p ex.backtrace ;TI"K # ["test.rb:4:in 'Object#parse_payload'", "test.rb:20:in '<main>'"] ;TI" p ex.backtrace_locations ;TI"K # ["test.rb:4:in 'Object#parse_payload'", "test.rb:20:in '<main>'"] ;TI" end ;T:@format0o; ; [I"EWhen the desired stack of locations is not available and should ;TI"Dbe constructed from scratch, an array of strings or a singular ;TI"Cstring can be used. In this case, only #backtrace is affected:;T@o;; [I"def parse_payload(text) ;TI" JSON.parse(text) ;TI"$rescue JSON::ParserError => ex ;TI"9 ex.set_backtrace(["dsl.rb:34", "framework.rb:1"]) ;TI"5 # The error have the new value in #backtrace: ;TI" p ex.backtrace ;TI") # ["dsl.rb:34", "framework.rb:1"] ;TI" ;TI"6 # but the original one in #backtrace_locations ;TI" p ex.backtrace_locations ;TI"F # [".../json/common.rb:221:in 'JSON::Ext::Parser.parse'", ...] ;TI" end ;TI" ;TI"&parse_payload('{"wrong: "json"') ;T;0o; ; [I"OCalling #set_backtrace with +nil+ clears up #backtrace but doesn't affect ;TI"#backtrace_locations:;T@o;; [I"def parse_payload(text) ;TI" JSON.parse(text) ;TI"$rescue JSON::ParserError => ex ;TI" ex.set_backtrace(nil) ;TI" p ex.backtrace ;TI" # nil ;TI" p ex.backtrace_locations ;TI"F # [".../json/common.rb:221:in 'JSON::Ext::Parser.parse'", ...] ;TI" end ;TI" ;TI"&parse_payload('{"wrong: "json"') ;T;0o; ; [I"QOn reraising of such an exception, both #backtrace and #backtrace_locations ;TI"&is set to the place of reraising:;T@o;; [I"def parse_payload(text) ;TI" JSON.parse(text) ;TI"$rescue JSON::ParserError => ex ;TI" ex.set_backtrace(nil) ;TI" raise # test.rb, line 7 ;TI" end ;TI" ;TI"begin ;TI"( parse_payload('{"wrong: "json"') ;TI"rescue => ex ;TI" p ex.backtrace ;TI"K # ["test.rb:7:in 'Object#parse_payload'", "test.rb:11:in '<main>'"] ;TI" p ex.backtrace_locations ;TI"K # ["test.rb:7:in 'Object#parse_payload'", "test.rb:11:in '<main>'"] ;TI" end ;T;0o; ; [I"9See {Backtraces}[rdoc-ref:exceptions.md@Backtraces].;T: @fileI"error.c;T:0@omit_headings_from_table_of_contents_below0I"#set_backtrace(value) -> value ;T0[ I" (p1);T@�FI"Exception;TcRDoc::NormalClass00