Reputation: 633
I have a function that handles an exception, and later I want to know if exception occurs. Here is my code:
def cx_runquery(query, conn_obj, fetch='all'):
"""
Run Query on the database
:param query: Query to execute (str)
:param conn_obj: Connection Object (obj)
:param fetch: Parameter with default='all' (str)
"""
cur = conn_obj.cursor()
try:
cur.execute(query)
if fetch == 'all':
return cur.fetchall()
elif fetch == 'one':
return cur.fetchone()
else:
raise ValueError('not a vaild option')
except cx_Oracle.DatabaseError as e:
error, = e.args
return error
Above function called and return data or error to the variable like following:
output = cx_runquery('SELECT somerow FROM sometable', ora_conn, fetch='one')
If an error occurs (for example ORA-00942: table or view does not exist
) then output
variable is cx_Oracle._Error
object.
I wrote the following test, but I don't know it's good approach?
if isinstance(output, cx_Oracle._Error):
output = 'Database errror occured'
In the pseudo code I want to do following:
if cx_Oracle._Error occured:
do something ...
else
do something ...
How to checking whether an exception (cx_Oracle._Error
in this case) occurred?
Upvotes: 1
Views: 706
Reputation: 2708
Do not include the try block in the function. Use try block where you want to catch it!
def cx_runquery(query, conn_obj, fetch='all'):
"""
Run Query on the database
:param query: Query to execute (str)
:param conn_obj: Connection Object (obj)
:param fetch: Parameter with default='all' (str)
"""
cur = conn_obj.cursor()
cur.execute(query)
if fetch == 'all':
return cur.fetchall()
elif fetch == 'one':
return cur.fetchone()
else:
raise ValueError('not a vaild option')
Then use it like:
try:
output = cx_runquery('SELECT somerow FROM sometable', ora_conn, fetch='one')
except cx_Oracle.DatabaseError as e:
output = "Database error occurred"
# do other error handling
Upvotes: 1
Reputation: 3114
IMO the best way to achieve is to raise the exception from child function and catch it in parent function. like -
def parent_function(query, conn_obj, fetch):
try:
cx_runquery(query, conn_obj, fetch='all')
#no error do something
except:
#error occured do somthing
def cx_runquery(query, conn_obj, fetch='all'):
"""
Run Query on the database
:param query: Query to execute (str)
:param conn_obj: Connection Object (obj)
:param fetch: Parameter with default='all' (str)
"""
cur = conn_obj.cursor()
try:
cur.execute(query)
if fetch == 'all':
return cur.fetchall()
elif fetch == 'one':
return cur.fetchone()
else:
raise ValueError('not a vaild option')
except cx_Oracle.DatabaseError as e:
error, = e.args
raise
So in parent_function
you can perform different actions on different behavior.
Even you can raise your custom exception or New exception from child function like
raise Excetion('some exception')
Also you can raise the same type cx_Oracle.DatabaseError
exception from child function and in parent function catch that specific type of exception and do required actions
Upvotes: 1
Reputation: 6264
In Python, you can return multiple results from a function. You might think about returning results, error
where one of those values is None
. That way, the client code can check explicitly if an error condition exists. This is especially useful if you start to encounter exceptions that are not of the type cx_Oracle._Error
.
The other approach would be to simply let the exception go uncaught and let your calling code deal with the error. This is a more idiomatic approach, as you really can't do anything useful with the error within the function itself. Leave it up to the client what happens when an exception is raised.
Upvotes: 1