first, Abnormal
What is an exception?
Is the event that affects the normal execution of the program, the exception occurs during the execution of the program, when the script is abnormal, we need to capture it, or the program Terminates.
Exception handling:
1. Catch exception: try: ... except .....
1 whileTrue:2 Try:3Num1=input ('>>:')4num2 = input ('>>:')5Num1=int (num1)#error, no longer executes the code block inside the try, directly into the except6num2 =int (num2)7num=num1+num28 Print(num)9 exceptException as E:Ten Print(e)#capture Error message program Re-execution
2. Complex exception handling Structure:
Try
Code Block 1
Except
Code Block 2
Else
Code block 3
Finally
Code Block 4
No exception: 1-3-4
Out Exception: 1-2-4
3. Abnormal type
Exception can catch all the exceptions
Other common exceptions
1 Attributeerror attempts to access a tree that does not have an object, such as foo.x, but Foo has no attribute x2IOError input/output exception: basically, The file cannot be opened3 Importerror cannot introduce modules or packages; it is basically a path problem or a name error4 indentationerror syntax error (subclass); code not aligned correctly5The indexerror index exceeds the sequence boundary, for example, when X has only three elements, but tries to access the X[5]6 Keyerror attempts to access keys that do not exist in the dictionary7Keyboardinterrupt Ctrl +C is pressed8 Nameerror using a variable that has not been assigned to an object9 syntaxerror python code is illegal, code can not compile (personally think this is a syntax error, write Wrong)Ten TypeError Incoming object types are not compliant with the requirements one Unboundlocalerror attempts to access a local variable that has not yet been set, basically because there is another global variable with the same Name. a cause you think you're accessing it -ValueError Pass in a value that is not expected by the caller, even if the value is of the correct type
exceptions for special handling or reminders need to be defined first, and finally defined exception to ensure that the program runs correctly
1S1 ='Hello'2 Try:3 int (s1)4 exceptIndexerror as E:5 Printe6 exceptKeyerror as E:7 Printe8 exceptValueError as E:9 PrinteTen exceptException as E: one PrintE
proactively triggering exceptions
Raise ........
1 whileTrue:2 Try:3Num1=input ('>>:')4num2 = input ('>>:')5num1=int (num1)6num2 =int (num2)7num=num1+num28 Print(num)9 RaiseException ('eeeeeeeeeerrrrrrrrrrrrrrssssssss')#proactively triggering exceptionsTen exceptException as E: one Print(e)#Print out Eeeeeeeeeerrrrrrrrrrrrrrssssssss
Assertion : Assert condition
1 Print (1) 2 assert 1==13print(2)4" output: 15 26"
second, Reflection
1, allowing users to enter different urls:
account/ Login
account/logout
Home/index
home/order
2, see different results
3, Enter a URL that does not exist, exposure 404 error
Span style= "font-size:15px" >url list =[account/login,account/logout,home/index,home/order]
above URL functions are saved in the account script under controller
inp=input (' Please enter URL ')
m,n=inp.split ('/') #m = Account N=login
Go to a module to find a function, if the function exists on the execution function, if there is no function to error
getattr () #专门用来去某个地方获取内部的东西 (obtained as a String)
1 fromControllerImportAccount#The functions of the above URLs are stored in the account script under the controller2 3H=hasattr (account,'Login1')#go to account module (script), whether the login function exists4 Print(h)#presence returns True no return false exists5GetAttr (account,'Login')#go to the account module (script) to get the login function6 7SetAttr (account,'Login1','DDD')#Add the Login1 function to the account module (script)8H=hasattr (account,'Login1')#again to determine if the Login1 attribute exists9 Print(h)#returns TrueTen oneDelattr (account,'Login1')#Remove the Login1 function from the account module (script) aH=hasattr (account,'Login1')#again to determine if the Login1 attribute exists - Print(h)#returns false
Hasattr (container, ' name ') to determine whether an object contains a specified property in the form of a string
GetAttr (container, ' name ') to get the specified property in the form of a string to an object
SetAttr (container, ' name ', Value) sets the specified property in the form of a string to an object
Delattr (container, ' name ') removes the specified property from an object in the form of a string
Python-exception Handling