ValueError code example
Example 1: python exception list
AssertionError
AttributeError
EOFError
FloatingPointError
GeneratorExit
ImportError
IndexError
KeyError
KeyboardInterrupt
MemoryError
NameError
NotImplementedError
OSError
OverflowError
ReferenceError
RuntimeError
StopIteration
SyntaxError
IndentationError
TabError
SystemError
SystemExit
TypeError
UnboundLocalError
UnicodeError
UnicodeEncodeError
UnicodeDecodeError
UnicodeTranslateError
ValueError
ZeroDivisionError
Example 2: how to print error in try except python
try:
except Exception as e:
print("ERROR : "+str(e))
Example 3: exception types python
BaseException
] SystemExit
] KeyboardInterrupt
] GeneratorExit
] Exception
] StopIteration
] StopAsyncIteration
] ArithmeticError
| ] FloatingPointError
| ] OverflowError
| ] ZeroDivisionError
] AssertionError
] AttributeError
] BufferError
] EOFError
] ImportError
| ] ModuleNotFoundError
] LookupError
| ] IndexError
| ] KeyError
] MemoryError
] NameError
| ] UnboundLocalError
] OSError
| ] BlockingIOError
| ] ChildProcessError
| ] ConnectionError
| | ] BrokenPipeError
| | ] ConnectionAbortedError
| | ] ConnectionRefusedError
| | ] ConnectionResetError
| ] FileExistsError
| ] FileNotFoundError
| ] InterruptedError
| ] IsADirectoryError
| ] NotADirectoryError
| ] PermissionError
| ] ProcessLookupError
| ] TimeoutError
] ReferenceError
] RuntimeError
| ] NotImplementedError
| ] RecursionError
] SyntaxError
| ] IndentationError
| ] TabError
] SystemError
] TypeError
] ValueError
| ] UnicodeError
| ] UnicodeDecodeError
| ] UnicodeEncodeError
| ] UnicodeTranslateError
] Warning
] DeprecationWarning
] PendingDeprecationWarning
] RuntimeWarning
] SyntaxWarning
] UserWarning
] FutureWarning
] ImportWarning
] UnicodeWarning
] BytesWarning
] ResourceWarning
Example 4: type error in python
try:
...
except SomeException:
tb = sys.exc_info()[2]
raise OtherException(...).with_traceback(tb)
Example 5: index error in python
import datetime
from gw_utility.book import Book
from gw_utility.logging import Logging
def main():
try:
books = list()
books.append(Book("Shadow of a Dark Queen", "Raymond E. Feist", 497, datetime.date(1994, 1, 1)))
books.append(Book("Rise of a Merchant Prince", "Raymond E. Feist", 479, datetime.date(1995, 5, 1)))
books.append(Book("Rage of a Demon King", "Raymond E. Feist", 436, datetime.date(1997, 4, 1)))
Logging.line_separator('Books')
log_list(books)
Logging.line_separator('Books w/ index')
log_list(books, True)
Logging.line_separator('books[len(books)]')
Logging.log(f'books[{len(books)}]: {books[len(books)]}')
except IndexError as error:
Logging.log_exception(error)
except Exception as exception:
Logging.log_exception(exception, False)
def log_list(collection, include_index=False):
"""Logs the each element in collection to the console.
:param collection: Collection to be iterated and output.
:param include_index: Determines if index is also output.
:return: None
"""
try:
for index, item in enumerate(collection):
if include_index:
Logging.log(f'collection[{index}]: {item}')
else:
Logging.log(item)
except IndexError as error:
Logging.log_exception(error)
except Exception as exception:
Logging.log_exception(exception, False)
if __name__ == "__main__":
main()
Example 6: python raise indexError
raise indexError