Name

try

Examples
element_names = {'H': 'hydrogen', 'He': 'helium', 'Li': 'lithium'}

try:
  print(element_names['Zz'])
  
except KeyError as e:
  print("caught KeyError: " + e.message) # Prints 'caught KeyError: Zz'
Description The try keyword is used with except to handle exceptions. When an exception is thrown inside a try block, the code inside the except block with a matching exception class name is run. If an as clause is given, the exception itself will be available inside this class as an object with the specified temporary name. Most exception objects have an attribute message, which can be accessed to further explain the condition that caused the exception to be raised.
Syntax
try:
  tryStatements
except exception as tmpName:
  exceptStatements
Parameters
tryStatementsif this code throws an exception, then the code in "except" is run
exceptionthe exception that was thrown
tmpNamea temporary name for the exception (available in exception-handling block)
exceptStatementscode that handles the exception
Related except

Updated on Tue Feb 27 14:07:12 2024.

If you see any errors or have comments, please let us know.