繁体   English   中英

如何处理 Azure Python Function 异常处理?

[英]How to handle Azure Python Function exception handling?

我是 Python 异常处理的新手。 我如何正确try以下操作, except .get_entity失败,但如果Status 200则通过?

这是我所在的位置:

  • 它虽然不正确。 希望你能举例说明。
from azure.cosmosdb.table.tableservice import TableService
from azure.cosmosdb.table.models import Entity
from azure.common import AzureMissingResourceHttpError

def get_table_row(TableName, PartitionKey, RowKey):
    try:
        table_lookup = table_service.get_entity(TableName, PartitionKey, RowKey)
    except AzureMissingResourceHttpError as e:
        logging.error(f'#### Status Code: {e.status_code} ####')
    finally:
        if e.status_code == 200:
            return table_lookup
        else:
            logging.error(f'#### Status Code: {e.status_code} ####')

data = get_table_row(TableName, PartitionKey, RowKey)

您可以更改您的代码,如下所示:

def get_table_row(TableName, PartitionKey, RowKey):
    try:
        table_lookup = table_service.get_entity(TableName, PartitionKey, RowKey)
    except AzureMissingResourceHttpError as e:
        if e.status_code == 200:
            return table_lookup
        else:
            logging.error(f'#### Status Code: {e.status_code} ####')
            return "whatever you want"
    else:
        return table_lookup

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM