204217 Computer Programming Languages Python Lecture 3 A

  • Slides: 10
Download presentation
204217: Computer Programming Languages (Python) Lecture 3 A Errors and Exceptions Assembled for 204217

204217: Computer Programming Languages (Python) Lecture 3 A Errors and Exceptions Assembled for 204217 by Kittipitch Kuptavanich

204217: Computer Programming Languages (Python) Exception Types • Python ม Built-in Exception Types หลายชนด

204217: Computer Programming Languages (Python) Exception Types • Python ม Built-in Exception Types หลายชนด (นอกจากนเรายงสามารถกำหนด Exception ชนดใหมๆ เพมไดเอง ) • Exception มาตรฐานทพบบอยไดแก Import. Error >>> import whatever Traceback (most recent call last): File "<stdin>", line 1, in <module> import whatever Import. Error: No module named 'whatever' Import. Error 4

204217: Computer Programming Languages (Python) Exception Types [2] Index. Error >>> a = "hello"

204217: Computer Programming Languages (Python) Exception Types [2] Index. Error >>> a = "hello" >>> a[5] Traceback (most recent call last): File "<stdin>", line 1, in <module> a[5] Index. Error: string index out of range Index. Error Name. Error >>> x + 4 Traceback (most recent call last): File "<stdin>", line 1, in <module> x + 4 Name. Error: name 'x' is not defined Name. Error 5

204217: Computer Programming Languages (Python) Exception Types [3] Type. Error >>> '2'+ 2 Traceback

204217: Computer Programming Languages (Python) Exception Types [3] Type. Error >>> '2'+ 2 Traceback (most recent call last): File "<stdin>", line 1, in <module> '2'+ 2 Type. Error: Can't convert 'int' object to str implicitly Value. Error >>> import math >>> math. sqrt(-6) Traceback (most recent call last): File "<stdin>", line 1, in <module> math. sqrt(-6) Value. Error: math domain error Value. Error 6

204217: Computer Programming Languages (Python) Handling Exceptions [2] • เราใชคำสง try และ except เพอการทำ

204217: Computer Programming Languages (Python) Handling Exceptions [2] • เราใชคำสง try และ except เพอการทำ Exception Handling 08 09 10 11 12 13 14 15 16 17 import math Might raise x = float(input("enter a number: ")) Value. Error try: print("Sqrt of ", x, "is: ", math. sqrt(x)) except Value. Error: print("Sqrt of a negative number is not supported") print("Using absolute value instead") print("Sqrt of ", -x, "is: ", math. sqrt(-x)) Exception Handling 8

204217: Computer Programming Languages (Python) Handling Exceptions [3] • Another Example 19 def read.

204217: Computer Programming Languages (Python) Handling Exceptions [3] • Another Example 19 def read. Int(): 20 while True: 21 val = input('Enter an integer: ') 22 try: 23 val = int(val) 24 return val 25 except Value. Error: 26 print(val, 'is not an integer') • Loop จนกวา User จะปอนคา Input ทถกตอง 9

204217: Computer Programming Languages (Python) References • https: //docs. python. org/3/tutorial/errors. html • https:

204217: Computer Programming Languages (Python) References • https: //docs. python. org/3/tutorial/errors. html • https: //docs. python. org/3/library/exceptions. html • https: //wiki. python. org/moin/Handling. Exceptions • Guttag, John V. Introduction to Computation and Programming Using Python, Revised • Miller & Ranum , Problem Solving with Algorithms and Data Structures Using Python 10