ข้อยกเว้นและการจัดการข้อผิดพลาด
แนะนำใหม่
5:16
การเขียนโปรแกรมเชิงวัตถุ
This course is about how a small script grows into a system that other people can safely change. The first two units build the object: classes, references and…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| exception/ekˈsepʃn/ | ข้อยกเว้น |
| status value/ˈsteɪtəs ˈvæljuː/ | ค่าสถานะ |
The loan that quietly shrank
- A renewal function returns $-1$ when a loan would pass its limit. One caller forgets to check and adds $-1$ to the loan, so a 20-day loan becomes 19 days.
- No error, no message: the failure spread silently. Exceptions exist so that a failure cannot be ignored.
Raise, catch, clean up
- Exceptions 异常 carry an error up to the code that can decide what to do.
raisethrows one;try/exceptcatches it by type. finallyruns on every path, whether the body succeeded or failed.
A small, precise hierarchy
- A custom hierarchy lets callers catch exactly what they can handle: a broad base and specific children. A handler for a parent also catches every child.
class LoanError(Exception): ...
class RenewalError(LoanError): ...
class LimitError(RenewalError): ...
except RenewalError ดัก LimitError ที่ถูกยกขึ้นเพราะ —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ตัวจัดการจับตามประเภทผ่านลำดับชั้น: ตัวจับ parent ทั่วไปจะจับ child ได้ทุกชนิด
except LimitError จะจับ RenewalError ที่ถูกยกขึ้นโดยตรง
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ตัวจัดการจะจับประเภทของมันเองและลูกหลานของมัน RenewalError เป็น parent ของ LimitError ดังนั้นจึงไม่ถูกจับ
Check first, raise loudly
renew(Loan(20), 15)checks before changing anything and raisesLimitError('20+15 exceeds 30').- The loan still reads 20. Because a failed call changed nothing, no rollback is needed.
loan = Loan(20) ผู้เรียกใช้รัน renew(loan, 15) ภายใน try block และจับ LimitError ค่าที่ loan.days อ่านได้หลังจากนี้คืออะไร?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
renew ตรวจสอบก่อนเปลี่ยนอะไรเลย ดังนั้นการเรียกที่ไม่สำเร็จจะทำให้ 20 คงอยู่เดิม
Unwinding the stack
- A raise leaves the current function and climbs frame by frame, from
renewthroughissueup tomain, running eachfinallyon the way, until anexceptmatches by type. - If nothing matches, the program stops with a traceback: loud, not silent.

Cleanup that always happens
- On the failing path
finallyruns after the handler; on the success path it runs straight after the body. - So cleanup that must happen, such as releasing a lock or closing a file, goes in
finallyor awithblock, never after the last statement.
block finally ทำงาน —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
finally คือการรับประกันการทำความสะอาด: มันทำงานหลัง body, หลัง handler, ก่อนการกลับค่า
Status values or exceptions?
- Use a status value 状态值 (such as
Nonefor "not found") for expected outcomes the caller handles right away. - Use an exception for a broken contract or a failure the immediate caller cannot fix.
ค่าที่ส่งกลับมาเป็น -1 ในฐานะรหัสข้อผิดพลาดนั้นอันตรายเพราะ —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ค่าปกติไม่ได้บังคับอะไร; ข้อยกเว้นไม่สามารถละเลยได้ — มันจะ unwind จนกว่าจะถูกจับ
จับคู่แต่ละสถานการณ์กับเครื่องมือที่เหมาะสม
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ผลลัพธ์ที่คาดหวังคือการคืนค่า; สัญญาที่ล้มเหลวจะยกข้อผิดพลาด; การทำความสะอาดอยู่ใน finally
Worked example: cleanup on the failing path
- Trace this with the 30-day limit:
locked = True
loan = Loan(28)
try:
renew(loan, 10)
except RenewalError:
kept = 'handled'
finally:
locked = False
- $28 + 10 = 38 > 30$, so
renewraisesLimitError, a kind ofRenewalError; the handler setskept;finallyunlocks. The loan still reads 28.
ในตัวอย่างที่แสดงขั้นตอน, renew จะยกข้อผิดพลาดภายใน try block. เรียงลำดับสิ่งที่เกิดขึ้น
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ตัวจัดการทำงานก่อน, จากนั้น finally, แล้วจึงกลับสู่การทำงานปกติ
Three common slips
- A bare
except:. It even catches Ctrl-C and hides real bugs. except ...: pass. The failure vanishes and the state may be half-changed.- Catching
Exceptionwhen you meant one specific error.
Recap
- Raise on broken contracts; catch where a decision can be made; a parent handler catches its children.
- Check first, so a failed call leaves the state unchanged.
finally(orwith) runs on every path.
| Status value | Exception | |
|---|---|---|
| For | an expected outcome, such as "not found" | a broken contract or a failure |
| Handled by | the immediate caller | whoever can decide |
| If ignored | the program runs on wrongly | the program stops loudly |
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้