本文介绍了如何处理Azure Python Function异常处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我在这里:

  • 这是不正确的.希望您能举例说明.
从azure.cosmosdb.table.tableservice导入

 从azure.cosmosdb.table.models导入实体从azure.common导入AzureMissingResourceHttpErrordef get_table_row(TableName,PartitionKey,RowKey):尝试:table_lookup = table_service.get_entity(TableName,PartitionKey,RowKey)除了AzureMissingResourceHttpError为e:logging.error(f'####状态代码:{e.status_code} ####')最后:如果e.status_code == 200:返回table_lookup别的:logging.error(f'####状态代码:{e.status_code} ####')数据= get_table_row(TableName,PartitionKey,RowKey) 
解决方案

您可以如下更改代码:

  def get_table_row(TableName,PartitionKey,RowKey):尝试:table_lookup = table_service.get_entity(TableName,PartitionKey,RowKey)除了AzureMissingResourceHttpError为e:如果e.status_code == 200:返回table_lookup别的:logging.error(f'####状态代码:{e.status_code} ####')返回无论您想要什么"别的:返回table_lookup 

I'm new to Python exception handling. How do I correctly try the following, except if .get_entity fails, but pass if Status 200?

Here is where I'm at:

  • Its not correct though. Hoping you could elaborate with an example.

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)
解决方案

You can change your code like below:

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

这篇关于如何处理Azure Python Function异常处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 03:47