Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcf92f17

Browse files
committed
Add function for Raising exception.
1 parent9a77d60 commitcf92f17

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

‎ExceptionIntro/src/exceptionintro.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def simple_exception_demo():
1818
else:
1919
forlineinfh:
2020
print(line.strip())
21-
21+
2222
defexception_demo():
2323
print('Notice:\nIn try block, the code execution stops at the line where exception occurs.\
2424
The further code is not executed.')
@@ -40,6 +40,23 @@ def exception_demo_specific_exception():
4040
forlineinfh:
4141
print(line.strip())
4242

43+
defraise_exception_demo():
44+
print('Demonstration of raising an exception in python.')
45+
46+
try:
47+
forlineinreadfile('sample.doc'):print(line.strip())
48+
exceptIOErrorase:
49+
print('File cannot be found,',e)
50+
exceptValueErrorase:
51+
print('Bad filename.',e)
52+
53+
defreadfile(filename):
54+
iffilename.endswith('.txt'):
55+
fh=open(filename)
56+
returnfh.readlines()# This function returns an iterator
57+
else:
58+
raiseValueError('Filename must end with\'.txt\'. Please check the file format.')
59+
4360
defmain():
4461
simple_exception_demo()
4562
print()
@@ -48,5 +65,7 @@ def main():
4865
exception_demo_specific_exception()
4966
print()
5067
read_file_n_display()
68+
print()
69+
raise_exception_demo()
5170

5271
if__name__=='__main__':main()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp