首页 > 解决方案 > 新手引发异常

问题描述

这是我第一次尝试编写除发那科 CNC 控制器以外的任何东西。我不知道为什么,但这段代码返回两个异常,如果我从 else 中删除 raise Exception:它不会返回任何一个异常。我错过了什么?

cut_type=[input("Enter cut type Straight(S) or Arc(A)" )]# Add Taper later

try:
    if 'S' in cut_type:
        valid_type=['Y']
    elif 's' in cut_type:
        valid_type=['Y']
    elif 'A' in cut_type:
        valid_type=['Y']
    elif 'a' in cut_type:
        valid_type=['Y']
    else:
        valid_type=['N']
        raise Exception('Cut Type not Valid')
except:
    print('NOT VALID TYPE')
    raise Exception('Cut Type not Valid')
print(valid_type)

如果我运行上面的代码,我会得到以下

Enter cut type Straight(S) or Arc(A)q
NOT VALID TYPE
Traceback (most recent call last):
  File "Chip Break testing.py", line 21, in <module>
    raise Exception('Cut Type not Valid')
Exception: Cut Type not Valid

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "Chip Break testing.py", line 24, in <module>
    raise Exception('Cut Type not Valid')
Exception: Cut Type not Valid

如果我在从任何一个位置删除 raise Exception 的情况下运行它,我不会得到任何异常。我知道我错过了一些简单的东西,但我看不到它。

标签: pythonexception

解决方案


这是我在提出建议并重新查看我的代码以使其正常工作后所做的。

cut_type=[input("Enter cut type Straight(S) or Arc(A)" )]# Add Taper later
 
if 'S' in cut_type:
    pass    
elif 's' in cut_type:
    pass    
elif 'A' in cut_type:
    pass    
elif 'a' in cut_type:
    pass    
else:
    raise Exception('Cut Type not Valid')

谢谢大家。我希望能学到更多知识并转行。


推荐阅读