[Wtr-general] A small doubt in exception handling
Charley Baker
charley.baker at gmail.com
Sat Mar 3 11:51:45 EST 2007
Sure, read the documentation for Ruby Exception class on rubydoc:
http://www.ruby-doc.org/core/
The first listed method is backtrace which has filename:lineno
def method_foo
begin
f = File.open('/some/non-existent/file')
rescue
puts "Message: " + $!.to_s
$!.backtrace.each {|l| puts l.to_s}
end
end
Message: No such file or directory - /some/non-existent/file
test.rb:4:in `initialize'
test.rb:4:in `open'
test.rb:4:in `method_foo'
test.rb:13
-Charley
On 3/3/07, vijay <forum-watir-users at openqa.org> wrote:
>
> Hello people,
>
> Is there a way in Watir by which one can capture the line number of the
> code that threw an exception? We can get the actual message in the
> exception from the gloabl variable '$!'. Can we get the code's line number,
> which caused this exception, from some other global variable?
>
> For example, the following code:
>
> begin
> print z
> rescue
> puts "The exception message was " + $!.to_s
> end
>
> would output the message,
>
> 'The exception message was undefined local variable or method `z' for
> #<Object:0x2ac9258>'.
>
> But without the 'begin -- rescue class;, the code would have printed the
> error message,
>
> 'file_name.rb:117 (or some other line number): undefined local variable or
> method `z' for #<Object:0x2ac9258> (NameError)
>
> I want to use the 'begin -- rescue class' and still get the exact line
> number along with the error message. Is that possible?
>
> Thanks for your time,
> Vijay.
> ---------------------------------------------------------------------
> Posted via Jive Forums
> http://forums.openqa.org/thread.jspa?threadID=6777&messageID=19510#19510
> _______________________________________________
> Wtr-general mailing list
> Wtr-general at rubyforge.org
> http://rubyforge.org/mailman/listinfo/wtr-general
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/wtr-general/attachments/20070303/078e3809/attachment.html
More information about the Wtr-general
mailing list