Reputation: 111
The message in FileNotFoundError does not print, it works fine for the ValueError, below is the code to implement this:
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)
The output below prints the Value error message but not the FileNotFoundError:
FileNotFoundError:
[Errno 2] No such file or directory: 'C://Users/Documents/abcde.txt'
ValueError:
['bob|1|23\n']
Hello
Upvotes: 0
Views: 533
Reputation: 57033
The FileNotFoundError
exception is raised in the statement with open(path) as f:
. The control is immediately passed to the except FileNotFoundError as e:
part, which prints the exception. Your code raise FileNotFoundError("Wrong file or file path")
is not executed.
Upvotes: 1