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 |
| ||||||||
Related |
try |
Updated on Mon Sep 21 15:53:24 2020.
If you see any errors or have comments, please let us know.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License