python manually trigger exception 1

python manually trigger exception

def demo_bad_catch():
    try:
        raise ValueError('Represents a hidden bug, do not catch this')
        raise Exception('This is the exception you expect to handle')
    except Exception as error:
        print('Caught this error: ' + repr(error))

>>> demo_bad_catch()
Caught this error: ValueError('Represents a hidden bug, do not catch this',)

Here is what the above code is Doing:
1. The function demo_bad_catch() raises a ValueError exception.
2. The function demo_bad_catch() raises an Exception exception.
3. The code in the except block catches the ValueError exception.
4. The code in the except block prints the error message.

The problem is that the code in the except block is catching the ValueError exception, but it’s not catching the Exception exception.

The code in the except block should be catching the Exception exception, not the ValueError exception.

The code in the except block should be catching the Exception exception, not the ValueError exception.

Similar Posts