0

I'm a python beginner. Two quick questions on the code below:

  1. Why can't I ever execute the print("printed valueError")?
  2. Why does the else statement with print("no error occurred") will print no matters what I put in?

The code:

def int_checker(a,b):
try:
    if isinstance(a,int) and isinstance(b,int):
        print('both integers')
    else:
        raise ValueError
        print('printed ValueError')
except:
    print('error occurred')
else:
    print('no error occurred')
finally:
    print('no error occurred')

print(int_checker(1,2))
print(int_checker(1,'a')
6
  • 1
    Your indentation is incorrect as posted please correct it Commented May 21, 2018 at 11:08
  • 2
    print('printedValueError') is right after a raise statement. When the exception is raised, execution goes to the except block, not to the next line. So that line is unreachable. Commented May 21, 2018 at 11:10
  • As it is now, it is finally statement where no error occured is printed. Commented May 21, 2018 at 11:10
  • 1
    And print('no error occurred') is in a finally block. The point of a finally block is that it should always be executed regardless of what happens in your try block. Commented May 21, 2018 at 11:11
  • Possible duplicate of How to use "raise" keyword in Python Commented May 21, 2018 at 11:12

2 Answers 2

1

Why can't I ever execute the print("printed valueError")?

It is simply because the code execution will raise ValueError exception and that jumps directly to the except part. To resolve, You can switch lines:

else:
    print('printed ValueError')
    raise ValueError

Why does the else statement with print("no error occurred") will print no matters what I put in?

You are using syntax with try, catch, else, finally. The order of those is:

  1. Run code in try block.
  2. Did some error occured?
    • True: execute code in catch block
    • False: execute code in else block
  3. Run code in finally block. (This will happen always!)

From the above, the code form finally statement will be run everytime no matter if there was or wasn't error during execution. In most cases it is used e.g. when You open file in try block, read from it and suddenly an error occurs. In that case You want to close the file and clear the references. That code will go to the finally block.


Similar question on SO: Purpose of else and finally in exception handling

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! Quick question -- when does the 2nd else statement get executed? Seems like it gets executed only when both inputs are integers. If one of the input is not an integer, we execute the except statement, and then jumps to finally else: print('no error occurred')
0

Anything in the "finally" block will print every time.

Try this: remove the 'else' from "try" block and add the following to the 'except' block:

print('printed ValueError')
raise ValueError

The 'else' after 'except' will only run if no errors occur

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.