1AssertionError #Raised when assert statement fails.
2AttributeError #Raised when attribute assignment or reference fails.
3EOFError #Raised when the input() functions hits end-of-file condition.
4FloatingPointError #Raised when a floating point operation fails.
5GeneratorExit #Raise when a generator's close() method is called.
6ImportError #Raised when the imported module is not found.
7IndexError #Raised when index of a sequence is out of range.
8KeyError #Raised when a key is not found in a dictionary.
9KeyboardInterrupt #Raised when the user hits interrupt key (Ctrl+c or delete).
10MemoryError #Raised when an operation runs out of memory.
11NameError #Raised when a variable is not found in local or global scope.
12NotImplementedError #Raised by abstract methods.
13OSError #Raised when system operation causes system related error.
14OverflowError #Raised when result of an arithmetic operation is too large to be represented.
15ReferenceError #Raised when a weak reference proxy is used to access a garbage collected referent.
16RuntimeError #Raised when an error does not fall under any other category.
17StopIteration #Raised by next() function to indicate that there is no further item to be returned by iterator.
18SyntaxError #Raised by parser when syntax error is encountered.
19IndentationError #Raised when there is incorrect indentation.
20TabError #Raised when indentation consists of inconsistent tabs and spaces.
21SystemError #Raised when interpreter detects internal error.
22SystemExit #Raised by sys.exit() function.
23TypeError #Raised when a function or operation is applied to an object of incorrect type.
24UnboundLocalError #Raised when a reference is made to a local variable in a function or method, but no value has been bound to that variable.
25UnicodeError #Raised when a Unicode-related encoding or decoding error occurs.
26UnicodeEncodeError #Raised when a Unicode-related error occurs during encoding.
27UnicodeDecodeError #Raised when a Unicode-related error occurs during decoding.
28UnicodeTranslateError #Raised when a Unicode-related error occurs during translating.
29ValueError #Raised when a function gets argument of correct type but improper value.
30ZeroDivisionError #Raised when second operand of division or modulo operation is zero.
1BaseException
2 ] SystemExit
3 ] KeyboardInterrupt
4 ] GeneratorExit
5 ] Exception
6 ] StopIteration
7 ] StopAsyncIteration
8 ] ArithmeticError
9 | ] FloatingPointError
10 | ] OverflowError
11 | ] ZeroDivisionError
12 ] AssertionError
13 ] AttributeError
14 ] BufferError
15 ] EOFError
16 ] ImportError
17 | ] ModuleNotFoundError
18 ] LookupError
19 | ] IndexError
20 | ] KeyError
21 ] MemoryError
22 ] NameError
23 | ] UnboundLocalError
24 ] OSError
25 | ] BlockingIOError
26 | ] ChildProcessError
27 | ] ConnectionError
28 | | ] BrokenPipeError
29 | | ] ConnectionAbortedError
30 | | ] ConnectionRefusedError
31 | | ] ConnectionResetError
32 | ] FileExistsError
33 | ] FileNotFoundError
34 | ] InterruptedError
35 | ] IsADirectoryError
36 | ] NotADirectoryError
37 | ] PermissionError
38 | ] ProcessLookupError
39 | ] TimeoutError
40 ] ReferenceError
41 ] RuntimeError
42 | ] NotImplementedError
43 | ] RecursionError
44 ] SyntaxError
45 | ] IndentationError
46 | ] TabError
47 ] SystemError
48 ] TypeError
49 ] ValueError
50 | ] UnicodeError
51 | ] UnicodeDecodeError
52 | ] UnicodeEncodeError
53 | ] UnicodeTranslateError
54 ] Warning
55 ] DeprecationWarning
56 ] PendingDeprecationWarning
57 ] RuntimeWarning
58 ] SyntaxWarning
59 ] UserWarning
60 ] FutureWarning
61 ] ImportWarning
62 ] UnicodeWarning
63 ] BytesWarning
64 ] ResourceWarning
1try:
2 # Code to test / execute
3 print('Test')
4except (SyntaxError, IndexError) as E: # specific exceptions
5 # Code in case of SyntaxError for example
6 print('Synthax or index error !')
7except :
8 # Code for any other exception
9 print('Other error !')
10else:
11 # Code if no exception caught
12 print('No error')
13finally:
14 # Code executed after try block (success) or any exception (ie everytime)
15 print('Done')
16
17# This code is out of try / catch bloc
18print('Anything else')
1# main.py
2import datetime
3
4from gw_utility.book import Book
5from gw_utility.logging import Logging
6
7
8def main():
9 try:
10 # Create list and populate with Books.
11 books = list()
12 books.append(Book("Shadow of a Dark Queen", "Raymond E. Feist", 497, datetime.date(1994, 1, 1)))
13 books.append(Book("Rise of a Merchant Prince", "Raymond E. Feist", 479, datetime.date(1995, 5, 1)))
14 books.append(Book("Rage of a Demon King", "Raymond E. Feist", 436, datetime.date(1997, 4, 1)))
15
16 # Output Books in list, with and without index.
17 Logging.line_separator('Books')
18 log_list(books)
19 Logging.line_separator('Books w/ index')
20 log_list(books, True)
21 # Output list element outside bounds.
22 Logging.line_separator('books[len(books)]')
23 Logging.log(f'books[{len(books)}]: {books[len(books)]}')
24 except IndexError as error:
25 # Output expected IndexErrors.
26 Logging.log_exception(error)
27 except Exception as exception:
28 # Output unexpected Exceptions.
29 Logging.log_exception(exception, False)
30
31
32def log_list(collection, include_index=False):
33 """Logs the each element in collection to the console.
34
35 :param collection: Collection to be iterated and output.
36 :param include_index: Determines if index is also output.
37 :return: None
38 """
39 try:
40 # Iterate by converting to enumeration.
41 for index, item in enumerate(collection):
42 if include_index:
43 Logging.log(f'collection[{index}]: {item}')
44 else:
45 Logging.log(item)
46 except IndexError as error:
47 # Output expected IndexErrors.
48 Logging.log_exception(error)
49 except Exception as exception:
50 # Output unexpected Exceptions.
51 Logging.log_exception(exception, False)
52
53
54if __name__ == "__main__":
55 main()
56