Help with Error Handling in Python

kevincalori Posts 2 Registration date Friday August 9, 2024 Status Member Last seen August 27, 2024 - Updated on Sep 8, 2024 at 08:36 AM

Hello everyone,

I am facing a problem with my Python script when trying to handle exceptions. I am using a try/except block to catch errors;  but instead of catching the specific exception I am expecting, the script seems to bypass the block entirely and raises an unhandled exception.

try:

    # Some operation that could raise an error

    result = my_function()

except ValueError as e:

    print(f"ValueError caught: {e}")

Despite expecting a ValueError, I get an error message like this:

TypeError: my_function() missing 1 required positional argument

Can anyone help me understand why this is happening and how I can confirm that the correct exception is caught?

I have referred https://docs.python.org/3/tutorialsplunk/errors.html documentation guide but still need help .

Could it be related to the way my function is structured? Any advice for error handling in Python would be greatly appreciated!

Thanks in advance.

Related: