How to print FULL stacktrace of exception w/ line #?

The ruby interpreter prints out a full trace of the exception.

However, all the expection I got is an one line message. I need to know
where the exception happened in my code. I don't mind it is verbose.

begin
....
Math.sqrt (-1)
rescue => e
puts e.inspect
end

e only prints out a message.

Am I missing anything real simple?

Thanks
~Andrew Chen

···

--
Posted via http://www.ruby-forum.com/.

Quoth Andrew Chen:

The ruby interpreter prints out a full trace of the exception.

However, all the expection I got is an one line message. I need to know
where the exception happened in my code. I don't mind it is verbose.

begin
....
Math.sqrt (-1)
rescue => e
puts e.inspect
end

e only prints out a message.

Am I missing anything real simple?

Thanks
~Andrew Chen

Don't rescue it, full stack trace! :stuck_out_tongue:

···

--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertillnoon.com/

begin
  ....
  Math.sqrt (-1)
rescue => e
  puts e.inspect
  puts e.backtrace
ensure
  andrew.brain << open("ri"){|ri| ri.read('Exception')}
end

···

On 9/19/07, Andrew Chen <meihome@gmail.com> wrote:

The ruby interpreter prints out a full trace of the exception.

However, all the expection I got is an one line message. I need to know
where the exception happened in my code. I don't mind it is verbose.

You can check utilrb's #full_message at
http://www.laas.fr/~sjoyeux/darcs/utilrb/lib/utilrb/exception/full_message.rb

(utilrb is also available as a gem on rubyforge :))

···

--
Sylvain Joyeux

Friends i want to write backtreace inside teardown method but i am
unable to get the syntax. Here is the script that i have

def teardown
if @check==1
$driver.close
puts "passed if condition"
else
puts "passed else condtion"
  $driver.save_screenshot('IM.png')
  sleep 3
      #------ i want to put backtrace here---
  $driver.switch_to.default_content
  sleep 1
  puts "Switched back from Iframe"

  $driver.find_element(:name, "headerForm:logoutLink").click
  puts "User successfully logged out"
  sleep 2

  $driver.close
  puts "test case passed"

      assert_equal [], @verification_errors
      $driver.save_screenshot('IM.png')
    puts "test case failed"
end

···

--
Posted via http://www.ruby-forum.com/.

Konrad Meyer wrote:

Don't rescue it, full stack trace! :stuck_out_tongue:

In the situation, I want to rescue, and run some other logic, since I
don't want the whole class to die.

···

--
Posted via http://www.ruby-forum.com/\.

Don't rescue it, full stack trace! :stuck_out_tongue:

... or not
Ruby removes in-betweeen lines if a backtrace is 'too long'. The limit is
not configurable, so it you have long backtraces (and if you need what is
in the middle) you're fucked.

···

--
Sylvain Joyeux

Michael Fellinger wrote in post #555437:

···

On 9/19/07, Andrew Chen <meihome@gmail.com> wrote:

The ruby interpreter prints out a full trace of the exception.

However, all the expection I got is an one line message. I need to know
where the exception happened in my code. I don't mind it is verbose.

begin
  ....
  Math.sqrt (-1)
rescue => e
  puts e.inspect
  puts e.backtrace
ensure
  andrew.brain << open("ri"){|ri| ri.read('Exception')}
end

hahah :slight_smile: nice one

--
Posted via http://www.ruby-forum.com/\.

"caller" will return the stack trace as an array. For example:

     puts caller

will print out the stack trace with file names, line numbers, and method names.

If you need to rescue an exception, you at least need a "rescue" clause[1].

For example:

def teardown
# ...
rescue => e
   puts e.backtrace
end

-Justin

[1]https://github.com/ruby/ruby/blob/trunk/doc/syntax/exceptions.rdoc

···

On 12/05/2013 11:23 PM, saurav panda wrote:

Friends i want to write backtreace inside teardown method but i am
unable to get the syntax. Here is the script that i have

def teardown
if @check==1
$driver.close
puts "passed if condition"
else
puts "passed else condtion"
   $driver.save_screenshot('IM.png')
   sleep 3
       #------ i want to put backtrace here---
   $driver.switch_to.default_content
   sleep 1
   puts "Switched back from Iframe"

   $driver.find_element(:name, "headerForm:logoutLink").click
   puts "User successfully logged out"
   sleep 2

   $driver.close
   puts "test case passed"

       assert_equal , @verification_errors
       $driver.save_screenshot('IM.png')
     puts "test case failed"
end

Justin Collins wrote in post #1129827:

   sleep 3
   puts "test case passed"

       assert_equal , @verification_errors
       $driver.save_screenshot('IM.png')
     puts "test case failed"
end

"caller" will return the stack trace as an array. For example:

     puts caller

will print out the stack trace with file names, line numbers, and method
names.

If you need to rescue an exception, you at least need a "rescue"
clause[1].

For example:

def teardown
# ...
rescue => e
   puts e.backtrace
end

-Justin

[1]https://github.com/ruby/ruby/blob/trunk/doc/syntax/exceptions.rdoc

justin
i have used this
def teardown
# ...
rescue => e
   puts e.backtrace
end

It is showing exception as un defined method backtrace

···

On 12/05/2013 11:23 PM, saurav panda wrote:

--
Posted via http://www.ruby-forum.com/\.

I miss Java.

I run into this kind of short sighted decision far too often with Ruby.
Ugh.

···

--
Posted via http://www.ruby-forum.com/.

Sylvain Joyeux wrote:

Don't rescue it, full stack trace! :stuck_out_tongue:

... or not
Ruby removes in-betweeen lines if a backtrace is 'too long'. The limit is not configurable, so it you have long backtraces (and if you need what is in the middle) you're fucked.

You can get that information when you rescue the exception - Exception#backtrace is an array of caller information.

···

--
Alex

e.backtrace works fine for me. In my case, I don't need traces in the
middle.

Thanks for all the help!

···

--
Posted via http://www.ruby-forum.com/.

Please check your code or post your exact code and error message.

$ irb
2.0.0p247 :001 > def teardown
2.0.0p247 :002?> raise
2.0.0p247 :003?> rescue => e
2.0.0p247 :004?> puts e.backtrace
2.0.0p247 :005?> end
  => nil
2.0.0p247 :006 > teardown
(irb):2:in `teardown'
(irb):6:in `irb_binding'
/home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb/workspace.rb:86:in `eval'
/home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb/workspace.rb:86:in `evaluate'
/home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb/context.rb:380:in `evaluate'
/home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb.rb:492:in `block (2 levels) in eval_input'
/home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb.rb:624:in `signal_status'
/home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb.rb:489:in `block in eval_input'
/home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb/ruby-lex.rb:247:in `block (2 levels) in each_top_level_statement'
/home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb/ruby-lex.rb:233:in `loop'
/home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb/ruby-lex.rb:233:in `block in each_top_level_statement'
/home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb/ruby-lex.rb:232:in `catch'
/home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb/ruby-lex.rb:232:in `each_top_level_statement'
/home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb.rb:488:in `eval_input'
/home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb.rb:397:in `block in start'
/home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb.rb:396:in `catch'
/home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb.rb:396:in `start'
/home/justin/.rvm/rubies/ruby-2.0.0-p247/bin/irb:12:in `<main>'
  => nil

-Justin

···

On 12/06/2013 12:06 AM, saurav panda wrote:

Justin Collins wrote in post #1129827:

On 12/05/2013 11:23 PM, saurav panda wrote:

    sleep 3
    puts "test case passed"

        assert_equal , @verification_errors
        $driver.save_screenshot('IM.png')
      puts "test case failed"
end

"caller" will return the stack trace as an array. For example:

      puts caller

will print out the stack trace with file names, line numbers, and method
names.

If you need to rescue an exception, you at least need a "rescue"
clause[1].

For example:

def teardown
# ...
rescue => e
    puts e.backtrace
end

-Justin

[1]https://github.com/ruby/ruby/blob/trunk/doc/syntax/exceptions.rdoc

justin
  i have used this
def teardown
# ...
rescue => e
    puts e.backtrace
end

It is showing exception as un defined method backtrace

Justin,
This is my entire code but u will not have access to use the application
link as it is an internal application.

require "rubygems"
require "selenium-webdriver"
gem "test-unit"
require "test/unit"

class Search_Export < Test::Unit::TestCase

def setup
$driver = Selenium::WebDriver.for :ie
$base_url = "https://qa-pec.razorsight.com/sso2"
$accept_next_alert = true
$driver.manage.timeouts.implicit_wait = 10
$verification_errors = []

#-----------------User Login-----------------
puts "User Log in"
$driver.get($base_url + "/login.jsf")
$driver.find_element(:id, "loginForm:username").clear
$driver.find_element(:id, "loginForm:username").send_keys
"sysadmin@firstcomm.com"
$driver.find_element(:id, "loginForm:password").clear
$driver.find_element(:id, "loginForm:password").send_keys "Saurav12345"
$driver.find_element(:id, "loginForm:loginBtn").click

puts "User has successfully logged in"
end

def test_Search_Export
assert_equal "Razorsight - Profit Enhancement Center", $driver.title
puts "Assertion for IMLink passed"

puts "Wait for 5sec for page to load"
sleep 5

$driver.switch_to.frame $driver.find_element(:id, "baseFrame")
puts "Switched to Iframe"

puts "Wait for 5sec for page load to complete"
sleep 5
puts "User Logged in"

#------------------------User Logged in------------------------
#-------------------Searching an Invoice-----------------------
@loop = 1
while @loop <= 2 do
$driver.find_element(:id, "j_id16:searchControlTab_lbl").click
$driver.find_element(:id, "j_id761:invoiceSearchForm:acntnp").clear

#-- this is error element ->j_id761:invoiceSearchForm:acntnp ---

$driver.find_element(:id, "j_id761:invoiceSearchForm:acntInp").send_keys
"80027309527"
#$driver.find_element(:css,
"td.rich-sb-cell-padding.richfaces_suggestionSelectValue").click
$driver.find_element(:id, "j_id761:invoiceSearchForm:srchBtn1").click
$driver.find_element(:id,
"j_id761:invoiceSearchForm:invoiceSearchDataTable:1:eachleminvumnvhvv4B8xt").click
puts "Searching done based on Entered Account Number"

sleep 1
#$driver.save_screenshot('IM.png')
$driver.find_element(:id, "j_id16:inventoryTab_lbl").click
sleep 1
$driver.find_element(:id,
"j_id966:provisionInventorySearch:srchBtn").click
sleep 1
puts "Inventory searched"

$driver.find_element(:id, "j_id16:searchControlTab_lbl").click
$driver.find_element(:id, "j_id761:invoiceSearchForm:acntnp").clear
$driver.find_element(:id, "j_id761:invoiceSearchForm:acntInp").send_keys
"80027309527"
#$driver.find_element(:css,
"td.rich-sb-cell-padding.richfaces_suggestionSelectValue").click
$driver.find_element(:id, "j_id761:invoiceSearchForm:srchBtn1").click
$driver.find_element(:id,
"j_id761:invoiceSearchForm:invoiceSearchDataTable:1:eachleminvumnvhvv4B8xt").click
puts "Searching done based on Entered Account Number"

$driver.find_element(:id, "j_id16:rzrMaintenanceTab_lbl").click
sleep 1
puts "Supplier list is searched"
@loop = @loop+1
end
#-----------Logging out----------

$driver.switch_to.default_content
sleep 1
puts "Switched back from Iframe"
@check=1
$driver.find_element(:name, "headerForm:logoutLink").click
puts "User successfully logged out"
end
#-----------Teardown Method... Here i want to catch the failed element
def teardown
if @check==1
$driver.close
puts "passed if condition"
else
puts "passed else condtion"
  $driver.save_screenshot('IM.png')
  sleep 3

  $driver.switch_to.default_content
  sleep 1
  puts "Switched back from Iframe"

  $driver.find_element(:name, "headerForm:logoutLink").click
  puts "User successfully logged out"
  sleep 2

  $driver.close
  puts "test case passed"

      assert_equal [], @verification_errors
      $driver.save_screenshot('IM.png')
    puts "test case failed"
end
end

end

···

--
Posted via http://www.ruby-forum.com/.

You can get that information when you rescue the exception -
Exception#backtrace is an array of caller information.

Yes, so I had to rescue all exceptions in all threads I spawn to have a
proper backtrace (even more so since with abort_on_exception the exception
is not shown /at all/).

Sometimes it drived me crazy ...

Sylvain Joyeux

You left out the important part, which is how you are adding rescue and what the error is when you try to do so.

-Justin

···

On 12/06/2013 12:43 AM, saurav panda wrote:

Justin,
This is my entire code but u will not have access to use the application
link as it is an internal application.

Justin Collins wrote in post #1129842:

Justin,
This is my entire code but u will not have access to use the application
link as it is an internal application.

You left out the important part, which is how you are adding rescue and
what the error is when you try to do so.

-Justin

C:\Users\saurav.panda\Desktop>ruby search__exportEH.rb
Loaded suite search__exportEH
Started
Started InternetExplorerDriver server (64-bit)
2.33.0.0
Listening on port 5555
User Log in
User has successfully logged in
Assertion for IMLink passed
Wait for 5sec for page to load
Switched to Iframe
Wait for 5sec for page load to complete
User Logged in
Searching done based on Entered Account Number
Inventory searched
E

···

On 12/06/2013 12:43 AM, saurav panda wrote:

===============================================================================
Error: test_Search_Export(Search_Export)
  Selenium::WebDriver::Error::NoSuchElementError: Unable to find element
with id == j_id761:invoiceSearchForm:acntnp
C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/response.rb:51:in
`assert_ok'
C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/response.rb:15:in
`initialize'
C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/common.rb:59:in
`new'
C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/common.rb:59:in
`create_response'
C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/default.rb:66:in
`request'
C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/common.rb:40:in
`call'
C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/bridge.rb:629:in
`raw_execute'
C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/bridge.rb:607:in
`execute'
C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/bridge.rb:575:in
`find_element_by'
C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/common/search_context.rb:42:in
`find_element'
search__exportEH.rb:62:in `test_Search_Export'
     59: puts "Inventory searched"
     60:
     61: $driver.find_element(:id, "j_id16:searchControlTab_lbl").click
  => 62: $driver.find_element(:id,
"j_id761:invoiceSearchForm:acntnp").clear
     63: $driver.find_element(:id,
"j_id761:invoiceSearchForm:acntInp").send_keys "80027309527"
     64: #$driver.find_element(:css,
"td.rich-sb-cell-padding.richfaces_suggestionSelectValue").click
     65: $driver.find_element(:id,
"j_id761:invoiceSearchForm:srchBtn1").click

passed else condtion
Switched back from Iframe
User successfully logged out
test case passed
F

Failure:
test_Search_Export(Search_Export)
search__exportEH.rb:110:in `teardown'
<> expected but was
<nil>

diff:
?
? nil

Finished in 78.243 seconds.

1 tests, 2 assertions, 1 failures, 1 errors, 0 pendings, 0 omissions, 0
notifications
0% passed

0.01 tests/s, 0.03 assertions/s

C:\Users\saurav.panda\Desktop>

--
Posted via http://www.ruby-forum.com/\.

Sylvain Joyeux wrote:

You can get that information when you rescue the exception -
Exception#backtrace is an array of caller information.

Yes, so I had to rescue all exceptions in all threads I spawn to have a
proper backtrace (even more so since with abort_on_exception the
exception
is not shown /at all/).

Sometimes it drived me crazy ...

Sylvain Joyeux

You might be able to rewrite the Thread.new method and have it catch and
display exceptions (and raise them).
GL!
-Roger

···

--
Posted via http://www.ruby-forum.com/\.

<snip>

Allow me to summarize this conversation:

1. You asked how to get a stack trace
2. I provided two methods, one using "caller" and the other getting the stack trace from an exception
3. You said you tried to use my code but got an error
4. I asked for how you tried my code and what the error looked like
5. You gave me your code WITHOUT any modifications or the error mentioned in step 3. However, you did ask a different question, how to get a "failed element" in the teardown method
6. I didn't see your different question, all I saw was that you didn't have "caller", "rescue", or "backtrace" anywhere in the code. So I requested the code that included one of these.
7. You gave me the output of running the webdriver code
8. Since we seem to be working at different problems, I wrote this list.

Going back to 6, I see what you want has to do with getting information from webdriver about a failure. You shouldn't do this in the teardown method, though, that's for resetting application state.

However, the information you need is right in the messages output from webdriver. So at this point I don't know what you want and we are back to the beginning.

At this point, I am going to bed. I hate to do this, but I'm going to have to direct you to How To Ask Questions The Smart Way and in particular How To Ask Questions The Smart Way

-Justin

···

On 12/06/2013 12:57 AM, saurav panda wrote:

Justin Collins wrote in post #1129842:

On 12/06/2013 12:43 AM, saurav panda wrote:

Justin,
This is my entire code but u will not have access to use the application
link as it is an internal application.

You left out the important part, which is how you are adding rescue and
what the error is when you try to do so.

-Justin

C:\Users\saurav.panda\Desktop>ruby search__exportEH.rb
Loaded suite search__exportEH