FileNotFoundError 消息未打印
FileNotFoundError message not printing
FileNotFoundError 中的消息不打印,它适用于 ValueError,下面是实现它的代码:
try:
with open(path) as f:
if header==True:
next(f)
for line in f:
try:
cnt += 1
a = line.count("|") + 1
if a == fields:
b.append(line)
yield b
else:
#raise ValueError(f"'{tail}' has {a} fields on line {cnt} but expected {fields}")
raise ValueError("Hello")
except ValueError as v:
print(v)
continue
raise FileNotFoundError("Wrong file or file path")
except FileNotFoundError as e:
print(e)
下面的输出打印了 Value 错误消息而不是 FileNotFoundError:
FileNotFoundError:
[Errno 2] No such file or directory: 'C://Users/Documents/abcde.txt'
值错误:
['bob|1|23\n']
Hello
语句 with open(path) as f:
中引发了 FileNotFoundError
异常。控制权立即传递给 except FileNotFoundError as e:
部分,该部分打印异常。您的代码 raise FileNotFoundError("Wrong file or file path")
未执行。
FileNotFoundError 中的消息不打印,它适用于 ValueError,下面是实现它的代码:
try:
with open(path) as f:
if header==True:
next(f)
for line in f:
try:
cnt += 1
a = line.count("|") + 1
if a == fields:
b.append(line)
yield b
else:
#raise ValueError(f"'{tail}' has {a} fields on line {cnt} but expected {fields}")
raise ValueError("Hello")
except ValueError as v:
print(v)
continue
raise FileNotFoundError("Wrong file or file path")
except FileNotFoundError as e:
print(e)
下面的输出打印了 Value 错误消息而不是 FileNotFoundError:
FileNotFoundError:
[Errno 2] No such file or directory: 'C://Users/Documents/abcde.txt'
值错误:
['bob|1|23\n']
Hello
语句 with open(path) as f:
中引发了 FileNotFoundError
异常。控制权立即传递给 except FileNotFoundError as e:
部分,该部分打印异常。您的代码 raise FileNotFoundError("Wrong file or file path")
未执行。