MikkiCoding
Search…
Welcome!
👩🏫
👩💻 Introduction of Programming
การทำงานของคอมพิวเตอร์เบื้องต้น
การแปลภาษาของโปรแกรม
Flowchart
Preprocesser Directory
Basic Coding
🐍
Python for Beginner
แนะนำภาษา python
ติดตั้งโปรแกรม python ลงบน computer
การพิมพ์คำสั่งขั้นต้น
Data type และการประกาศตัวแปร
Comparison
Condition Statement
Loop Statement
การใช้ break, continue และ pass ใน statement
Function
Data structure
การทำงานร่วมกันกับไฟล์
Exception handling
Document และแหล่งเรียนรู้อื่นๆ
Website
Github
Facebook Page
Twitter
Youtube
Powered By
GitBook
Exception handling
Syntax Error
คือ การที่ compiler แจ้งในส่วนที่เราเขียนโค้ดผิด syntax ที่เราพบเห็นกัน มีดังนี้
SyntaxError
: invalid syntax
IndexError
: string index out of range
NameError
: name 'n' is not defined
Exception
คือ การที่ compiler แจ้งเราว่า code error ไม่สามารถทำงานต่อได้ มี Error defect ดังนี้
ZeroDivisionError
: division by zero
NameError
: name 'test2' is not defined
TypeError
: unsupported operand type(s) for -: 'str' and 'int'
Handling Exception
การจัดการเมื่อโค้ดของเรามีปัญหา มี syntax การเขียน ดังนี้
1
try
:
2
//
Protected code
3
except
:
4
//
Catch block
5
else
:
6
//
The
finally
block always executes
.
Copied!
การทำงาน ทำในส่วนของ try ก่อน ถ้าทำงานในส่วนนี้สำเร็จ ก็คือจบ ถ้าไม่สำเร็จ จะไปในส่วนของ except ต่อ
Raising Exceptions
ถ้าเปรียบเทียบกับ JAVA เหมือน throw
1
raise
ValueError
(
'A very specific bad thing happened'
)
Copied!
ตัวอย่างการใช้งาน เช่น การหารด้วยศูนย์
1
try
:
2
result
=
input1
/
input2
3
except
ZeroDivisionError
:
4
print
(
“cannot divide by
0
”
)
Copied!
Python for Beginner - Previous
การทำงานร่วมกันกับไฟล์
Next - Python for Beginner
Document และแหล่งเรียนรู้อื่นๆ
Last modified
4mo ago
Copy link
Edit on GitHub
Contents
Syntax Error
Exception
Handling Exception
Raising Exceptions