เอกสารอ้างอิงมาตรฐานของ Python Exceptions: https://docs.python.org/2/library/exceptions.html สำหรับ Python 3: https://docs.python.org/3/library/exceptions.html
เอกสารอ้างอิงมาตรฐานของ Python Exceptions: https://docs.python.org/2/library/exceptions.html สำหรับ Python 3: https://docs.python.org/3/library/exceptions.html
IOError raised เมื่อ I/O operation ล้มเหลว (open file ไม่มี, print statement error) ตัวอย่าง import sys def whatever(): try: f = open(foo.txt, r) except IOError as e: print(e) print(sys.exc_type) whatever() ผลลัพธ์ [Errno 2] No such file or directory: foo.txt
ArithmeticError เป็น base class ของ OverflowError, ZeroDivisionError, FloatingPointError ตัวอย่าง import sys try: 7/0 except ArithmeticError as e: print(e) print(sys.exc_type) print(This is an example of catching ArithmeticError) ผลลัพธ์ integer division or modulo by zero This is a
OverflowError raised เมื่อ arithmetic operation เกินขีดจำกัดของ type (float มีขีดจำกัด, int แปลงเป็น long อัตโนมัติ) ตัวอย่าง i = 1 try: f = 3.0**i for i in range(100): print(i, f) f = f ** 2 except OverflowError as err: print(Overflowed after, f, err) ผลลัพธ์ Floating po
IndexError raised เมื่อ sequence index out of range ตัวอย่าง import sys try: my_list = [3, 7, 9, 4, 6] print(my_list[6]) # IndexError except IndexError as e: print(e) print(sys.exc_type) ผลลัพธ์ list index out of range
NameError raised เมื่ออ้างถึงชื่อที่ไม่มีใน scope ปัจจุบัน ตัวอย่าง import sys try: def foo(): print(magnolia) # undefined foo() except NameError as e: print(e) print(sys.exc_type) ผลลัพธ์ global name magnolia is not defined
EOFError raised เมื่อ input() หรือ raw_input() ไม่ได้อ่านข้อมูลก่อนเจอ EOF ตัวอย่าง # eofError.py try: while True: data = raw_input(prompt:) print(READ:, data) except EOFError as e: print(e) รัน: $ echo hello | python eofError.py prompt:READ: hello prompt:EOF when reading a l
SyntaxError raised เมื่อ parser ไม่เข้าใจ source code (import, exec, eval) ตัวอย่าง try: print(eval(six times seven)) except SyntaxError as err: print(Syntax error %s (%s-%s): %s % (err.filename, err.lineno, err.offset, err.text)) print(err) ผลลัพธ์ Syntax error (1-9): six times seven i
Python ต้องการ indentation consistent หากบรรทัดหนึ่ง indent 4 spaces บรรทัดถัดไป indent 3 หรือ 5 จะได้ IndentationError: unexpected indent ตัวอย่างผิด def a(): print foo print baz # indent ไม่เท่ากัน ตัวอย่างถูกต้อง def a(): print foo print baz ทุกบรรทัดใน block ต้อง indent เท่ากัน
TypeError raised เมื่อ combine object type ผิด หรือ call function ด้วย argument type ผิด ตัวอย่าง import sys try: ny = Statue of Liberty my_list = [3, 4, 5, 8, 9] print(my_list + ny) # TypeError except TypeError as e: print(e) print(sys.exc_type) ผลลัพธ์ can only concatenate li
EnvironmentError เป็น base class ของ IOError และ OSError (errors จาก OS, filesystem) ไม่ได้ raise โดยตรง แต่ subclasses raise ตัวอย่าง IOError (subclass) import sys try: f = open(JohnDoe.txt, r) except Exception as e: print(e) print(sys.exc_type) ผลลัพธ์ [Errno 2] No such file or direct
LookupError เป็น base class ของ IndexError และ KeyError ตัวอย่าง IndexError import sys try: foo = [a, s, d, f, g] print(foo[5]) # IndexError except IndexError as e: print(e) print(sys.exc_type) ผลลัพธ์ list index out of range
ValueError raised เมื่อฟังก์ชันรับค่าที่ type ถูกต้องแต่ value ไม่ถูกต้อง ตัวอย่าง import sys try: n = int(magnolia) except Exception as e: print(e) print(sys.exc_type) ผลลัพธ์ invalid literal for int() with base 10: magnolia
เกิดเมื่อหารด้วยศูนย์ ตัวอย่าง import sys try: x = 11/0 print(x) except Exception as e: print(sys.exc_type) print(e) ผลลัพธ์ integer division or modulo by zero
FloatingPointError raised เมื่อ floating point operation error และ fpectl เปิดอยู่ (ต้อง compile Python ด้วย --with-fpectl) ตัวอย่าง import sys import math import fpectl try: print(Control off:, math.exp(700)) fpectl.turnon_sigfpe() print(Control on:, math.exp(1000)) except Exception as
StandardError เป็น base class ของ built-in exceptions หลายตัว (ArithmeticError, AttributeError, AssertionError, etc.) ตัวอย่าง: AttributeError (subclass ของ StandardError) import sys try: class Foobar: def __init__(self): self.p = 0 f = Foobar() print(f.p) p
Iterator หมดแล้ว next() จะ raise StopIteration (ไม่ใช่ error) ตัวอย่าง import sys try: z = [5, 9, 7] i = iter(z) print(i) print(i.next()) print(i.next()) print(i.next()) print(i.next()) # raise StopIteration except Exception as e: print(e) print(sys.exc_type) ผล
SystemExit ไม่ใช่ subclass ของ Exception แต่เป็น subclass ของ BaseException วิธีที่ 1: Catch BaseException try: raise SystemExit except BaseException: print(It works!) วิธีที่ 2: Catch SystemExit โดยตรง (แนะนำ) try: raise SystemExit except SystemExit: print(It works!) ผลลัพธ์ It work
ImportError raised เมื่อ import module หรือ member ไม่ได้ กรณี 1: Module ไม่มีอยู่ import sys try: from exception import myexception except Exception as e: print(e) print(sys.exc_type) ผลลัพธ์ No module named exception กรณี 2: Module มีแต่ member ไม่มี import sys try: from time impo
User-defined base classes raise NotImplementedError บังคับ subclass override method (simulate interface) ตัวอย่าง import sys try: class Super(object): @property def example(self): raise NotImplementedError(Subclasses should implement this!) s = Super() p