As in Python 3, with try, execute the command, but in except, output an error and continue the execution of the program.

Example:

error= ' ' try: print (str(1/0)) except: error = 'ошибка Π½ΠΎΠΌΠ΅Ρ€ ....' print("ошибка с тСкстом, строка, линия ΠΈ Ρ‚.ΠΏ. Ρ‚ΠΈΠΏ ошибки(Ρ‡Ρ‚ΠΎ ΠΏΠΈΡˆΠ΅Ρ‚ компилятор): "+str(error)) #ΠΏΡ€ΠΎΠ΄ΠΎΠ»ΠΆΠ°Π΅Ρ‚ исполнСниС try: print (str(0/0)) except: error = 'ошибка Π½ΠΎΠΌΠ΅Ρ€ ....' print("ошибка с тСкстом, строка, линия ΠΈ Ρ‚.ΠΏ. Ρ‚ΠΈΠΏ ошибки(Ρ‡Ρ‚ΠΎ ΠΏΠΈΡˆΠ΅Ρ‚ компилятор): "+str(error)) #ΠΏΡ€ΠΎΠ΄ΠΎΠ»ΠΆΠ°Π΅Ρ‚ исполнСниС try: print (str(1+1)) except: error = 'ошибка Π½ΠΎΠΌΠ΅Ρ€ ....' print("ошибка с тСкстом, строка, линия ΠΈ Ρ‚.ΠΏ. Ρ‚ΠΈΠΏ ошибки(Ρ‡Ρ‚ΠΎ ΠΏΠΈΡˆΠ΅Ρ‚ компилятор): "+str(error)) 
  • one
    except Exception as exc: print(exc) for starters - andreymal
  • one
    And in general, just except: almost never need to write, since you risk losing the ability to shut down the program properly (SystemExit and KeyboardInterrupt exceptions are caught) - andreymal
  • Is it possible to display error messages of the type and how: - Yulenka
  • Traceback (most recent call last): File "D: \ 1 \ fb \ vremm_dla_prob.py", line 15, in <module> element = driver.find_element_by_id ("email") # find the field with email check_response raise exception_class (message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method": "id", "selector": "email"} (Session info: chrome = 70.0.3538.110) (Driver info: chromedriver = 2.43.600210 (68dcf5eebde37173d4027fa8635e332711d2874a), platform = Windows NT 6.1.7601 SP1 x86) - Yulia
  • one
    import traceback; traceback.print_exc () - andreymal 5:18

2 answers 2

A. You need to understand that if you do try except the code will be interrupted only if you explicitly interrupt its execution, for example, with the exit() in the except block. That is, in this design:

 aab = '123' b = 3 try: print(aab+b) except: print('123') print('Π― Π±Π΅Π³Ρƒ Π±Π°Π±Ρƒ ягу') print('Заяц - Π’ΠΎΠ»ΠΊ') 

In the case of "no error" , execute the try block and the rest of the code, outside the block try except . In case of an error , the except try except is executed, and then the code beyond the try except . In this particular example, the last two prints:

 print('Π― Π±Π΅Π³Ρƒ Π±Π°Π±Ρƒ ягу') print('Заяц - Π’ΠΎΠ»ΠΊ') 

will be executed in any case!

B. It is possible to display the text of the error in two main ways:

Declare except as a variable and this variable "print":

 except as err: print(err) 

enter image description here

--- OR ---

Import the traceback library and print the "full path" error:

 import traceback except: print(traceback.format_exc()) 

enter image description here

The first option prints only the "last" point of the error, most often this is enough. But sometimes, a detailed path is needed - traceback

Here you can read about try except - https://pythonworld.ru/tipy-dannyx-v-python/isklyucheniya-v-python-konstrukciya-try-except-dlya-obrabotki-isklyuchenij.html

     import traceback; error= ' ' try: print (str(1/0)) except Exception as exc: print(exc) error = 'ошибка Π½ΠΎΠΌΠ΅Ρ€ ....' print("ошибка с тСкстом, строка, линия ΠΈ Ρ‚.ΠΏ. Ρ‚ΠΈΠΏ ошибки(Ρ‡Ρ‚ΠΎ ΠΏΠΈΡˆΠ΅Ρ‚ компилятор): "+str(error)) print ("traceback.print_exc():") traceback.print_exc() print("____") #ΠΏΡ€ΠΎΠ΄ΠΎΠ»ΠΆΠ°Π΅Ρ‚ исполнСниС try: print (str(0/0)) except Exception as exc: print ("traceback.print_exc():") traceback.print_exc() print("____") print(exc) error = 'ошибка Π½ΠΎΠΌΠ΅Ρ€ ....' print("ошибка с тСкстом, строка, линия ΠΈ Ρ‚.ΠΏ. Ρ‚ΠΈΠΏ ошибки(Ρ‡Ρ‚ΠΎ ΠΏΠΈΡˆΠ΅Ρ‚ компилятор): "+str(error)) #ΠΏΡ€ΠΎΠ΄ΠΎΠ»ΠΆΠ°Π΅Ρ‚ исполнСниС try: print (str(1+1)) except Exception as exc: print(exc) error = 'ошибка Π½ΠΎΠΌΠ΅Ρ€ ....' print("ошибка с тСкстом, строка, линия ΠΈ Ρ‚.ΠΏ. Ρ‚ΠΈΠΏ ошибки(Ρ‡Ρ‚ΠΎ ΠΏΠΈΡˆΠ΅Ρ‚ компилятор): "+str(error)) 

    using @andreymal