Python try Keyword
The try
keyword is used in conjunction with the
except
keyword and often the
finally
keyword to wrap blocks of code
that may throw an error.
Example
Python
Copy Code
try: result = 10 / 0 except: print("Oops - division by 0!")
Output
Oops - division by 0!
By wrapping the potentially error-prone code in a try...except block we are able to catch the exception, do something with it (eg record the error) and the continue on without the program halting due to the unhandled error.