Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. วิธี Catch IOError Exception ใน Python

    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

  2. วิธี Catch ArithmeticError Exception ใน Python

    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

  3. วิธี Catch OverflowError Exception ใน Python

    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

  4. วิธี Catch IndexError Exception ใน Python

    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

  5. วิธี Catch NameError Exception ใน Python

    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

  6. วิธี Catch EOFError Exception ใน Python

    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

  7. วิธี Catch SyntaxError Exception ใน Python

    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

  8. Unexpected Indent ใน Python คืออะไร

    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 เท่ากัน

  9. วิธี Catch TypeError Exception ใน Python

    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

  10. วิธี Catch EnvironmentError Exception ใน Python

    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

  11. วิธี Catch LookupError Exception ใน Python

    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

  12. วิธี Catch ValueError ด้วย Exception ใน Python

    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

  13. วิธี Catch FloatingPointError Exception ใน Python

    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

  14. วิธี Catch StandardError Exception ใน Python

    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

  15. วิธี Catch StopIteration Exception ใน Python

    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) ผล

  16. วิธี Catch SystemExit Exception ใน Python

    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

  17. วิธี Catch ImportError Exception ใน Python

    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

  18. วิธี Catch NotImplementedError Exception ใน Python

    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

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:8/451  20-คอมพิวเตอร์/Page Goto:1 2 3 4 5 6 7 8 9 10 11 12 13 14