1

I wonder if there is a way to capture any (and all) type of error that crashes the script, but potentially without using try to catch a specific exception.

I have a webdriver script that does a lot of stuff, like clicks on links, sends keys and so on, but I cannot predict where it will crash. I want to run a series of tests in sequence and print results into a file - including any errors/crashes: Eg.

Test01 - Success!   
Test02 - NameError: name 'driver' is not defined !ERROR!
Test03 - Success!

and so on.

So that at the end I can just open the text file and check which tests need to be fixed and re-run.

Now I've been using try/exception with smaller blocks of code, but it seems to me that I would have to somehow include the whole script within try/catch/exception to get what I want.

Is there a better way to catch any type of error that is displayed after a script crashes, like on the image below:

enter image description here

Alichino
  • 1,668
  • 2
  • 16
  • 25
  • Possible duplicate of [Calling a hook function every time an Exception is raised](http://stackoverflow.com/questions/1029318/calling-a-hook-function-every-time-an-exception-is-raised) – acdr Jun 30 '16 at 14:32
  • @acdr I don't know how to do what Ken suggested in the post, ie. using the sys.excepthook, so I'm not sure if that question answers mine... – Alichino Jun 30 '16 at 14:49

2 Answers2

3

You can use a plain except to catch any and all exceptions, like so:

try:
    some code here
except:
    print 'whoops, something went wrong'

However this doesn't give you any information about the exception, so you probably don't want to do that.

You can also catch the very broad Exception, like so:

try:
    some code here
except Exception as ex:
    print 'oops, caught this error: %s' % ex

This is a better method as it tells you what exactly went wrong.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
0

It seems the trick you are looking for is redirecting the stderr to a file or stdout.

Python when having the exception will write the exception to stderr . simply you need to redirect stderr to the same output file you have when running the python program

like this

python file.py &> outputfile

Hani
  • 1,354
  • 10
  • 20
  • When I do that in ipython, the file is not created. In normal command line it says to me: "The syntax of the command is incorrect." – Alichino Jun 30 '16 at 15:16
  • check this question http://stackoverflow.com/questions/14571090/ipython-redirecting-output-of-a-python-script-to-a-file-like-bash. it will show you code you can use to redirect the output in iPython. So use that code and redirect both stderr and stdout to same file – Hani Jun 30 '16 at 15:32
  • That looks good, but I think it's too much messing about... :) I think I'll just print Test_Number: at the beginning, then just before driver.quit() I'll append with SUCCESS!, so that when any test crashes before reaching that line, I'll only see Test Number: and I'll know something went wrong. But without knowing what exactly. – Alichino Jun 30 '16 at 15:37
  • Honestly the easiest way is to wrap the code with try except like @John Gordon said. that is the least amount of work and the more correct solution (my solution is hacky based on your requirement of not using try) – Hani Jun 30 '16 at 15:51