12  Exception

Python Exception (realpython)

12.1 Raising Exception

x = 10
if x > 5:
    raise Exception('x should not exceed 5. The value of x was: {}'.format(x))
#> x should not exceed 5. The value of x was: 10

12.2 AssertionError Exception

import sys
assert ('linux' in sys.platform), "This code runs on Linux only."
#> This code runs on Linux only.

12.3 try and except Block

Python executes code following the try statement as a “normal” part of the program. The code that follows the except statement is the program’s response to any exceptions in the preceding try clause.

12.3.1 Ex: Linux function

def linux_interaction():
    assert ('linux' in sys.platform), "Function can only run on Linux systems."
    print('Doing something.')
def mac_interaction():
    assert ('darwin' in sys.platform), "Function can only run on MacOS systems."
    print('Doing something.')
try:
    linux_interaction()
except:
    print('Linux function was not executed')
#> Linux function was not executed
try:
    linux_interaction()
except AssertionError as error:
    print(error)
    print('The linux_interaction() function was not executed')
#> Function can only run on Linux systems.
#> The linux_interaction() function was not executed

12.3.2 Ex: Int & Float

def get_int(x):
    try:
        return int(x)
    except ValueError:
        print(f"'{x}' is not a number")
get_int(5)
#> 5
get_int(-1)
#> -1
get_int(5.6)
#> 5
get_int("cat")
#> 'cat' is not a number
def attempt_float(x):
    try:
        return float(x)
    except ValueError:
        return x
attempt_float("2")
#> 2.0
attempt_float("X")
#> 'X'

using pass to return NoneType and not print anything

def get_int2(x):
    try:
        return int(x)
    except ValueError:
        pass
get_int2(2)
#> 2
get_int2("Cat") # Nothing

type(get_int2("Cat"))
#> <class 'NoneType'>

isnumeric() to check whether string is all numeric. But, it’s not very effective:

"123".isnumeric() # Only case that works
#> True
"12.3".isnumeric()
#> False
"-1".isnumeric()
#> False
"cat".isnumeric()
#> False

12.3.3 Ex: Try open file

try:
    with open('file.log') as file:
        read_data = file.read()
except:
    print('Could not open file.log')
#> Could not open file.log
try:
    with open('file.log') as file:
        read_data = file.read()
except FileNotFoundError as fnf_error:
    print(fnf_error)
#> [Errno 2] No such file or directory: 'file.log'
try:
    linux_interaction() # <-- AssertionError trigger in this line
    with open('file.log') as file:
        read_data = file.read()
except FileNotFoundError as fnf_error:
    print(fnf_error)
except AssertionError as error: 
    print(error) 
    print('Linux linux_interaction() function was not executed')
#> Function can only run on Linux systems.
#> Linux linux_interaction() function was not executed
try:
    mac_interaction()
    with open('file.log') as file: # <-- FileNotFoundError trigger in this line
        read_data = file.read()
except FileNotFoundError as fnf_error:
    print(fnf_error)
except AssertionError as error:
    print(error)
    print('Linux mac_interaction() function was not executed')
#> Doing something.
#> [Errno 2] No such file or directory: 'file.log'

12.4 else clause

try:
    mac_interaction() # Pass
except AssertionError as error:
    print(error)
else:
    print('Executing the else clause.')
#> Doing something.
#> Executing the else clause.

12.4.1 try-except-else-finally

try:
    mac_interaction()
except AssertionError as error:
    print(error)
else:
    try:
        with open('file.log') as file:
            read_data = file.read()
    except FileNotFoundError as fnf_error:
        print(fnf_error)
finally:
    print('Cleaning up, irrespective of any exceptions.')
#> Doing something.
#> [Errno 2] No such file or directory: 'file.log'
#> Cleaning up, irrespective of any exceptions.

12.4.2 Always close File

f = open(path, mode="w")

try:
    write_to_file(f)
except:
    print("Failed")
else:
    print("Succeeded")
finally:
    f.close()

12.5 Custom Exception

Custom exceptions initialize by creating a class that inherits from the base Exception class of Python

class MyCustomException(Exception):
    pass
raise MyCustomException('A custom message for my custom exception')
#> A custom message for my custom exception
try:
    raise MyCustomException('A custom message for my custom exception')
except MyCustomException:
    print('My custom exception was raised')
#> My custom exception was raised