Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中捕获错误时,如何在异常中创建异常?_Python_Function_If Statement_Exception_Error Handling - Fatal编程技术网

在python中捕获错误时,如何在异常中创建异常?

在python中捕获错误时,如何在异常中创建异常?,python,function,if-statement,exception,error-handling,Python,Function,If Statement,Exception,Error Handling,所以,我有这个代码,工作非常好,但我想有一种可能性,如果我输入x,它将返回我选择列表的开始 def ask2(): while True: try: number = int(input('Pick a number in range 1-100: ')) except ValueError: # just catch the exceptions you know! print('That\'s not a number!') else

所以,我有这个代码,工作非常好,但我想有一种可能性,如果我输入x,它将返回我选择列表的开始

def ask2():
while True:
    try:
        number = int(input('Pick a number in range 1-100: '))
    except ValueError:  # just catch the exceptions you know!
        print('That\'s not a number!')
    else:
        if 1 <= number <= 100:  # this is faster
            print("added")
        else:
            print('Out of range. Try again')
def ask2():
尽管如此:
尝试:
number=int(输入('Pick a number in range 1-100:'))
除了ValueError:#只需捕获您知道的异常!
打印('这不是一个数字!')
其他:
如果1
Guido van Rossum花了一些时间与Java打交道,而Java
支持组合除块和最终块的等效项,
这澄清了该声明的含义:

执行
块1
中的代码。如果代码引发
异常
,则 测试除块之外的各种块:如果异常属于类
Exception1
,执行
handler-1
;否则的话,如果它是一流的
异常2
,执行
处理程序-2
,依此类推。如果没有例外 则执行
else块

无论之前发生了什么,最后一个块执行一次 代码块已完成,所有引发的异常都已处理。即使 异常处理程序或
else块中有错误
和新的 引发异常时,
最终块中的代码仍在运行



最后
?然后,当我最后添加时,它说可能会在赋值之前引用局部变量number初始化变量,然后我会将“continue”放在打印“added”的位置,以打破while True。但是,由于它位于函数内部,因此可以使用“return”代替。
try:
    block-1 ...
except Exception1:
    handler-1 ...
except Exception2:
    handler-2 ...
else:
    else-block
finally:
    final-block