#print (a); #NameError: Name ' A ' is not defined
#print (10/0);
#print (int ("a"));
# valueerror:invalid literal for int () with base: ' A ' value conversion is not valid
#-*-config=utf-8-*-#=========================== Exception Handling (capturing run-time exceptions) =====================#Common Exceptions#print (a); #NameError: Name ' A ' is not defined variable undefined#if True: #SyntaxError: Unexpected EOF while parsing syntax error#F=open ("123.txt"); #FileNotFoundError: [Errno 2] No such file or directory: ' 123.txt ' does not have this file#print (10/0); # Zerodivisionerror:division by zero divisor is 0 error#Print (int ("a")); # valueerror:invalid literal for int () with base: ' A ' Value conversion invalid#===================== try-except Grammar ==================deftrytest_1 ():Try: Print(10/0); except:#catch all types of exceptions Print("Zero Error");#Zero Error#trytest_1 ();deftrytest_2 ():Try: Print(10/0); exceptZerodivisionerror as E:#capturing exceptions of the specified type Print("Zero Error");#Zero Error#trytest_2 ();#case guessing number gameImportRandom;defguessnum (): Num=random.randint (0,100);#generate a random number whileTrue:Try: Inputnum=int (input ()); exceptValueError as E:Print("Please enter a number of 1-100"); Continue; ifNum>Inputnum:Print("the input is too small"); ifnum<Inputnum:Print("the input is too large"); if(num==inputnum):Print("Ok"); Break;#guessnum ();#====================== handles multiple exceptions at the same time ====================deftrytest_3 ():Try: F=open ("2.txt"); exceptFilenotfounderror as E:Print("file does not exist");#file does not exist exceptValueError as E:Print("Wrong value");#trytest_3 ();#===================== try-except-else==============#If there is an exception code that performs the corresponding exception type capture, if no exception code is executed elsedeftrytest_4 ():Try: Print(+); #F=open ("2.txt"); exceptFilenotfounderror as E:Print("file does not exist");#file does not exist exceptValueError as E:Print("Wrong value"); Else: Print("No Error");#Trytest_4 ();#===================== try finally ============#The finally code is executed regardless of whether the exception is checked#function: Provides a cleanup mechanism for exception handling events, which is used to close files or release resources. deftrytest_5 ():Try: F=open ("2.txt"); finally: Print("file Close"); F.close ();#trytest_5 ();#========================== try-except-finally =============#1. If the try statement does not catch an exception, execute the try code and then execute the finally statement#2. If the try statement catches an exception, execute the FINALLY statement after executing the EXCEPT statement firstdeftrytest_6 ():Try: F=open ("2.txt"); exceptFilenotfounderror as E:Print("file does not exist"); finally: Print("file Close"); F.close ();#trytest_6 ();#========================== try-except-else-finally =============#1. If the try statement does not catch an exception, execute the ELSE statement after executing the try code and finally execute the finally statement#2. If the try statement catches an exception, execute the FINALLY statement after executing the EXCEPT statement firstdeftrytest_7 ():Try: Print(10/1); exceptZerodivisionerror as E:#capturing exceptions of the specified type Print("Zero Error");#Zero Error Else: Print("Else Statement"); finally: Print("finally");#trytest_7 ();#==============================with Statement ======================#The WITH statement replaces the Try-except-finall statement to make the code more concisedefwithtest ():Try: With open ("E:\python\w_2.txt") as F:Print(F.readline ()); exceptFilenotfounderror as E:Print("file does not exist"); F.close ();#withtest ();#=======================raise Statement ==============================#raise actively throws an exception#similar to the throw keyword in Javadefraisetest ():RaiseIOError ("IO Exception");#raisetest ();#======================assert Statement =================================#Assert statement: Used to detect whether an expression is true or false to throw a assertionerror error#syntax: Assert expression (to judge an expression)#assert expression1, expression2 (judging multiple expressions)ImportRandom;defasserttest (n):assertN>random.randint (0,10); Print(n);#asserttest (3);#Prints N If the incoming n is greater than the randomly generated number#throws an assertionerror error if the incoming n is less than a randomly generated number
Python Learning anomalies