Pythonのraise ValueError

関数などで例外を発生させたい場合にraiseを使用する。

raise 例外クラス(message):

def process_list(numbers):
    for num in numbers:
        try:
            if num < 0:
                raise ValueError("Negative numbers are not allowed.")
            result = 100 / num
            print(f"Result of division: {result}")
        except ZeroDivisionError:
            print("Error: Division by zero is not allowed.")
        except ValueError as ve:
            print(f"Error: {ve}")
        except Exception as e:
            print(f"Error occurred: {e}")