Nested try/except statement for method
Question:
I wonder if it is possible to handle the exceptions raised when calling a method via a function (this is necessary as in the production code different objects are created depending on args passed) as in the following example.
Function createObj
triggers the creation of an object Obj_A
based off different criteria and is supposed to handle any exceptions that may occur with Obj_A
.
def createObj():
try:
return Obj_A()
except:
print("Bad boy!")
Obj_A
has a method that creates a nested object, in which I would like to catch exceptions and handle those at the level of createObj
:
class Obj_A(object):
def __init__(self):
pass
def myFunc(self, var):
return self.Obj_B(self, var)
class Obj_B(object):
def __init__(my, self, var):
try:
1/0
except:
raise ValueError("Don't divide by zero")
Calling createObj
works just fine. But calling createObj.myFunc('var')
raises the ValueError("Don't divide by zero")
.
Of course, handling the error on a
try:
createObj().myFunc('var')
except:
print("Not what I need")
would work, but is unfortunately not desirable for this use case.
Is there a way to handle this exception on the createObj
level and return Bad boy!
?
Answers:
You want to handle an exception with the except
clause that will be raised in the future after the corresponding try
statement. It’s impossible, and unnatural if it’s possible.
Instead, do in other way like this example.
def createObj():
def handle_exception(e):
print("Bad boy!")
return Obj_A(handle_exception)
class Obj_A(object):
def __init__(self, handle_exception):
self.handle_exception = handle_exception
def myFunc(self, var):
try:
return self.Obj_B(self, var)
except Exception as e:
self.handle_exception(e)
I wonder if it is possible to handle the exceptions raised when calling a method via a function (this is necessary as in the production code different objects are created depending on args passed) as in the following example.
Function createObj
triggers the creation of an object Obj_A
based off different criteria and is supposed to handle any exceptions that may occur with Obj_A
.
def createObj():
try:
return Obj_A()
except:
print("Bad boy!")
Obj_A
has a method that creates a nested object, in which I would like to catch exceptions and handle those at the level of createObj
:
class Obj_A(object):
def __init__(self):
pass
def myFunc(self, var):
return self.Obj_B(self, var)
class Obj_B(object):
def __init__(my, self, var):
try:
1/0
except:
raise ValueError("Don't divide by zero")
Calling createObj
works just fine. But calling createObj.myFunc('var')
raises the ValueError("Don't divide by zero")
.
Of course, handling the error on a
try:
createObj().myFunc('var')
except:
print("Not what I need")
would work, but is unfortunately not desirable for this use case.
Is there a way to handle this exception on the createObj
level and return Bad boy!
?
You want to handle an exception with the except
clause that will be raised in the future after the corresponding try
statement. It’s impossible, and unnatural if it’s possible.
Instead, do in other way like this example.
def createObj():
def handle_exception(e):
print("Bad boy!")
return Obj_A(handle_exception)
class Obj_A(object):
def __init__(self, handle_exception):
self.handle_exception = handle_exception
def myFunc(self, var):
try:
return self.Obj_B(self, var)
except Exception as e:
self.handle_exception(e)