#!/usr/bin/env python # coding: utf-8 # # [Exceptions](https://docs.python.org/3/library/exceptions.html#concrete-exceptions) # When something goes wrong an exception is raised. For example, if you try to divide by zero, `ZeroDivisionError` is raised or if you try to access a nonexistent key in a dictionary, `KeyError` is raised. # # # In[ ]: empty_dict = {} # empty_dict['key'] # Uncomment to see the traceback # ## `try-except` structure # If you know that a block of code can fail in some manner, you can use `try-except` structure to handle potential exceptions in a desired way. # In[ ]: # Let's try to open a file that does not exist file_name = "not_existing.txt" try: with open(file_name) as my_file: print("File is successfully open") except FileNotFoundError as e: print(f"Uups, file: {file_name} not found") print(f"Exception: {e} was raised") # If you don't know the type of exceptions that a code block can possibly raise, you can use `Exception` which catches all exceptions. In addition, you can have multiple `except` statements. # In[ ]: def calculate_division(var1, var2): result = 0 try: result = var1 / var2 except ZeroDivisionError as ex1: print("Can't divide by zero") except Exception as ex2: print(f"Exception: {ex2}") return result result1 = calculate_division(3, 3) print(f"result1: {result1}") result2 = calculate_division(3, "3") print(f"result2: {result2}") result3 = calculate_division(3, 0) print(f"result3: {result3}") # `try-except` can be also in outer scope: # In[ ]: def calculate_division(var1, var2): return var1 / var2 try: result = calculate_division(3, "3") except Exception as e: print(e) # ## Creating your custom exceptions # In your own applications, you can use custom exceptions for signaling users about errors which occur during your application run time. # In[ ]: import math # Define your own exception class NegativeNumbersNotSupported(Exception): pass # Dummy example how to use your custom exception def secret_calculation(number1, number2): if number1 < 0 or number2 < 0: msg = f"Negative number in at least one of the parameters: {number1}, {number2}" raise NegativeNumbersNotSupported(msg) return math.sqrt(number1) + math.sqrt(number2) # Uncomment to see the traceback # result = secret_calculation(-1, 1)