Name

except

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 except keyword is used with try 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 try
Updated on Tue Jul 11 06:52:23 2017.
Creative Commons License