Python Flask应用程序中未捕获自定义异常

2024-06-26 13:24:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个烧瓶应用程序调用不同的模块。其中一个最终会引发NoPrice异常,但except NoPrice没有将其作为NoPrice捕获,但except Exception将其捕获

class NoPrice(Exception):
    pass

模块中的某个地方

raise NoPrice('error')

在我的烧瓶应用程序中

try:
    raise NoPrice('main')
except NoPrice as e:
    print('main')
except Exception as e:
    print('main2')

try:
    resp = alicia.create_predictions(select)
except NoPrice as e:
    print('main3')
except Exception as e:
    print('main4')
    print(repr(e))
    print(e)
    print(traceback.format_exc())

这个的输出是

main
main4
NoPrice('No price found for material_reference 0148054b-e681-4fb6-9722-946f3cfa8529 the weight 1.7471266917293233', 'occurred at index 0')
('No price found for material_reference 0148054b-e681-4fb6-9722-946f3cfa8529 the weight 1.7471266917293233', 'occurred at index 0')
Traceback (most recent call last):
  File "c/main.py", line 71, in create_predictions_api
    resp = alicia.create_predictions(select)
  File "/absolutepath/app/alicia.py", line 703, in create_predictions
    self.set_machines_and_format()
  File "/absolutepath/app/alicia.py", line 574, in set_machines_and_format
    x["is_screenprinting_option"]), axis = 1)
  File "/absolutepath/venv/lib/python3.7/site-packages/pandas/core/frame.py", line 6913, in apply
    return op.get_result()
  File "/absolutepath/venv/lib/python3.7/site-packages/pandas/core/apply.py", line 186, in get_result
    return self.apply_standard()
  File "/absolutepath/venv/lib/python3.7/site-packages/pandas/core/apply.py", line 292, in apply_standard
    self.apply_series_generator()
  File "/absolutepath/venv/lib/python3.7/site-packages/pandas/core/apply.py", line 321, in apply_series_generator
    results[i] = self.f(v)
  File "/absolutepath/app/alicia.py", line 574, in <lambda>
    x["is_screenprinting_option"]), axis = 1)
  File "/absolutepath/app/alicia.py", line 359, in predict_price_relying_on_format
    output = Jerome(self.product, self.hipe_client_config).run(self.quote_input)
  File "/absolutepath/app/jerome/jerome.py", line 235, in run
    pliant = pliant_obj.price(quote_input, self.material_reference)
  File "/absolutepath/app/jerome/options_construction/pliant.py", line 66, in price
    quote_input['matter_margin_percentage'])
  File "/absolutepath/app/jerome/options_construction/pliant.py", line 52, in pliant
    raise NoPrice(f"No price found for material_reference {material_reference.id} the weight {x1}")
exceptions.NoPrice: ('No price found for material_reference 0148054b-e681-4fb6-9722-946f3cfa8529 the weight 1.7471266917293233', 'occurred at index 0')

为什么除了NoPrice没有发现我的异常

输出不应该是吗

main
main3

我的目标是最终只处理我的NoPrice异常


Tags: inpyselfapplinepricefilematerial