Hive UDF with Python - Print exception on shell - python

In the code below, Whenever exception is caught, I would like to exit from the program & print the exception on shell
#stream.py
import hashlib
import sys
import os
import importlib
for line in sys.stdin.readlines():
try:
inpFile = "temp.py"
execfile(inpFile)
line1 = line.strip('\n').split('\t')
print "\t".join(line1)
except:
#exception expected "temp.py:File Not Found", how do I exit the code & print the exception on console ?
sys.exit(1)
Here is the Transform query to call the UDF:
Create table newtable as Select TRANSFORM(id,name) USING
'python stream.py' as (id,name) from mytable;
ideas appreciated.

if you want to catch a specific type of exception (for examle an IOError) you can use
except IOError as e:
and access the error string with e.strerror
if you want to catch all exception you can use sys.exc_info()[0] to access the last error message
for example
try:
1/0
except:
print sys.exc_info()[0]
would print
<type 'exceptions.ZeroDivisionError'>

If you are expecting a specific exception then you should trap that, not everything, which is what you are doing. But there is nothing in your code to generate a "File Not Found" anyway!
EDIT: question code changed! We really do wish to trap "everything". I am using Exception, which traps "almost" everything, see https://docs.python.org/2/library/exceptions.html
This uses Python 2 syntax:
import sys
for line in sys.stdin.readlines():
try:
inpFile = "temp.py"
execfile(inpFile)
line1 = line.strip('\n').split('\t')
print "\t".join(line1)
except Exception as err:
print >> sys.stderr, err # print to stderr (python 2 syntax)
sys.exit(1)

Related

Is there an easy way to customize try-except error code outputs in python?

I am trying to custimize my error codes in a script that I am writing and I was wondering if I can adjust the output. Here is the scenario. Running this script:
while True:
try:
x = "Hello World"
int(x)
except (ValueError, RuntimeError, TypeError, NameError, SystemExit):
sys.exit("Error: Check Section 1.0")
Creates an output of:
An exception has occurred, use %tb to see the full traceback.
SystemExit: Error: Check Section 1.0
Is there an easy way, when an error occurs, the script stops and only says:
Error: Check Section 1.0
Just do print and do break since you keep it inside a loop
while True:
try:
x = "Hello World"
int(x)
except:
print("Error: Check Section 1.0")
break
Try this instead, this method direct shows you "Error: Check Section 1.0" only.
while True:
try:
x = "Hello World"
int(x)
except:
sys.exit("Error: Check Section 1.0")

How to print the reason for program exit / program termination / exit errors?

I managed to catch the termination with:
atexit.register(exitHandler)
But how to print out what happened on that point? I want to see if the program aborts due to an error, due to Crtl-C or normal stopping...
It's not the complete solution - but you could wrap code in try except
try:
YOUR CODE HERE
except Exception as e:
print(e)
You should catch KeyboardInterrupt for Ctrl-C. E.g.:
import sys
try:
# your code
except KeyboardInterrupt:
sys.exit('Abort by user interrupt')
except Exception as exc:
sys.exit(f'Abort on error: {exc}')

How do I exit Python if run from command line, otherwise print stack trace

I want to exit Python if I encounter an exception in my function if that function is being executed from the command line, but want to raise an exception and print a stack trace if the my function is not run from the command line.
Right now I have
try:
#...
except Exception as e:
print('ERROR: Some useful message')
if __name__ == '__main__':
raise SystemExit
else:
raise e
but I feel like I'm either doing too much here, or too little.
Is there an idiomatic way to get a stack trace with the original exception when my function is run from the command line; but simply exit if it is being run from the command line?
The better way would be to do this:
import sys
def func():
do_actual_processing()
if not successful:
raise Exception("Yadayada")
if __name__ == '__main__'
try:
func()
except Exception as e:
sys.exit(1)
That is, the function itself does not need to be concerned with whether or not it is run from command line.

Python requests exceptions timeout

I wrote a python script like this:
#!/usr/bin/python
import sys
import requests
if len(sys.argv) != 2:
print "Usage: python %s <IP-LIST>" % (sys.argv[0])
sys.exit();
InputFile = sys.argv[1]
try:
File = open(InputFile)
for ip in File:
IP = ip.rstrip()
out = requests.get("http://" + IP, timeout=5)
out.status_code
except (KeyboardInterrupt, SystemExit):
print "\nKeyboardInterruption with Ctrl+c signal"
except IOError as e:
print "The file \"%s\" does not exist!" % (sys.argv[1])
When url has nothing to respond the following output appears for me:
The file "list.txt" does not exist!
Why?
requests uses exceptions that subclass IOError, which you are catching and assuming to be file not found. If you were using Python 3 you could catch the more specific FileNotFoundError. Here you should put an except requests.RequestException block above your except IOError block (or break it out into specific requests errors if you want.

What is this featureless Python exception?

I have a script containing a section similar to this, on Python 2.6:
import sys
list_id='cow'
prev=[0,'cow']
try:
if list_id==prev[1]:
print '{0} is the same as {1}'.format(list_id,prev[1])
sys.exit(0)
except:
print 'exception occurred, exiting with error'
sys.exit(1)
I noticed that though it was printing the 'is the same' line, it also logs the exception!
If you remove the try/except block, the interpreter shows no error. If you catch a specific error like ValueError, the except block is not executed.
import sys
list_id='cow'
prev=[0,'cow']
try:
if list_id==prev[1]:
print '{0} is the same as {1}'.format(list_id,prev[1])
sys.exit(0)
except Exception as k:
print 'exception occurred, exiting with error. Exception is:'
print k.args
sys.exit(1)
The except block is not executed, and the process finishes with return code 0. So, the exception is above Exception in the hierarchy?
import sys
list_id='cow'
prev=[0,'cow']
try:
if list_id==prev[1]:
print '{0} is the same as {1}'.format(list_id,prev[1])
sys.exit(0)
except BaseException as k:
print 'exception occurred, exiting with error. Exception is:'
print k.args
sys.exit(1)
produces
cow is the same as cow exception occurred, exiting with error.
Exception is: (0,)
And the process finishes with exit code 1.
Why is this Except block being executed at all?
sys.exit() raises SystemExit, which is what you're seeing.
As to why it doesn't inherit from Exception:
The exception inherits from BaseException instead of StandardError or
Exception so that it is not accidentally caught by code that catches
Exception. This allows the exception to properly propagate up and cause the interpreter to exit.
sys.exit() simply raises SystemExit. That's how it exits the program. When you catch all exceptions, you also catch SystemExit.
In addition to other answers: SystemExit does not inherit from Exception, python exception hierarchy: http://docs.python.org/library/exceptions.html

Categories

Resources