如何根据if语句的条件提出特定的例外?
我有多个例外,只有在条件为真时才想提出特定的异常。你知道是否可能吗?
def lambda_handler(event, context):
verif = boto3.resource('iam')
client_iam = boto3.client('iam')
user = verif.User('Tom')
try:
user.load()
if verif.exceptions.NoSuchEntityException:
raise Userdoesnotexist
if ex.response['Error']['Code'] != 'NoSuchEntityException':
raise Userexists
except Userdoesnotexist:
except Userexists:
I have multiple exceptions and want to raise a specific exception only if a condition is true. Do you know if it would be possible ?
def lambda_handler(event, context):
verif = boto3.resource('iam')
client_iam = boto3.client('iam')
user = verif.User('Tom')
try:
user.load()
if verif.exceptions.NoSuchEntityException:
raise Userdoesnotexist
if ex.response['Error']['Code'] != 'NoSuchEntityException':
raise Userexists
except Userdoesnotexist:
except Userexists:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。应该有可能。例如,我从Python的错误和例外文档中找到了这一点。
在此示例中,例外已放在列表中。 并且例外是一个一个
该列表已在for循环中迭代,
。 stackoverflow.com/questions/1319615/proper-way-to-declare-custom-exceptions-in-Modern-python"> thread 。
Yes. it should be possible. as an example i found this from the Errors and Exceptions documentation of python.
In this example the exceptions are put on a list. The list is iterated in the for loop and the exceptions are raised one by one..
For your purpose maybe it would look something like:
Probably it is not perfect but you could take a look to this thread.