Даже когда мой блок try не выдает никакой ошибки, мой блок кроме выполняется каждый раз - PullRequest
0 голосов
/ 27 сентября 2019

У меня есть код, написанный в блоке try и catch, и хотя блок try не генерирует никаких исключений, код в блоке кроме выполняется.

def transform(code):
    try:
        dict_item = {}
        if len(code.strip())>0:
            str = '' 
            code = code.encode('utf-8')
            hash_object = hashlib.sha1(code)
            hex_dig = hash_object.hexdigest()
            request_code= code.split("POST request : ")[1]
            request_code = request_code.split(", response : ")
            request_json = json.loads(request_code[0])
            response_json = json.loads(request_code[1])
            order_code = request_json['saleOrder']['code']
            response = json.dumps(response_json)
            dict_item['id'] = hex_dig
            dict_item['order_code'] = order_code
            dict_item['response'] = response
            if response_json['errors']:
                for i in response_json['errors']:
                    str = str +i['description']+'|'
            dict_item['error'] = str   
            success_status = response_json['successful']
            dict_item['success_status'] = success_status       
            for k in dict_item.keys():
                if k not in ["id","order_code","response","error","success_status"]:
                    dict_item.pop(k)
    except Exception as e:
        print("error-------------------")
        print code
        print("hahah:"+e)
        print("error"+traceback.format_exc(limit=10))
        print("error-------------------")
    if len(dict_item)>0:
        return dict_item
    else:
        return None

Строка кода должна быть напечатана только в том случае, еслилюбое выполнение выполняется, но оно печатается каждый раз.

...