1

My question is where the difference between

try:
  print("Hello")
except:
  print("Something went wrong")
else:
  print("Nothing went wrong")

and

try:
  print("Hello")
  print("Nothing went wrong")
except:
  print("Something went wrong")

lies. The result seems identical to me. What are use cases to use the else statement?

Similarly, why would I ever use the finally statement?

try:
  print(x) #Not defined before
except:
  print("Something went wrong")
finally:
  print("The 'try except' is finished")

and

try:
  print(x) #Not defined before
except:
  print("Something went wrong")
print("The 'try except' is finished")

again seems identical.

Edit: I do know, what 'else' and 'finally' do. I just don't know when to use them as it seems you can reach an equivalent result without these statements.

  • It's pretty pointless using examples where no error is raised. Assuming that *every* print *could* raise an error, these do wildly different things. If you would replace some of the `print`s with `raise` this should become obvious. – MisterMiyagi Jan 15 '22 at 12:20
  • The finally always runs even in an expectation that stops execution, the finally block will run. So it always runs, whether it succeeds or fails. – Jarvis Jan 15 '22 at 12:22
  • Well, I do know, what 'else' and 'finally' does. It just doesn't make sense to me when I would use it, as I could just write the code of the 'else' statement within the 'try' section and get the same result. Same with 'finally'. Just leave it out and write the code after the try-except statement and it also always gets executed. – Philipp Kut Jan 15 '22 at 12:39

3 Answers3

0

The else block is only executes when no exception got raised. So

try:
    f_noexception()
    print("hello")
except Exception
   pass
else:
    print("Hello again")

Should give you two prints. Personallz, I think this is mildly useful and you actually don't see it being used very often. However, finally is much more useful. The finally block gets always executed no matter if an exception was raised or not. So you can use that for doing some clean ups for example. Especially when you don't just catch an exception and handle it, but want it to get raised.

Simon Hawe
  • 3,968
  • 6
  • 14
0

finally keyword in Python

  • finally block is always executed after leaving the try statement. In case if some exception was not handled by except block, it is re-raised after execution of finally block.

  • finally block is used to deallocate the system resources.

  • One can use finally just after try without using except block, but no exception is handled in that case.

  • finally block is executed regardless of exception is handled or not.

    try:
        print(x) #Not defined before
     except:   
         print("Something went wrong")
     finally:
         #this block is always executed 
         # regardless of exception generation.
         print('This is always executed')
    

Here, if your except block executes or not, finally block is always executed.

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

Answering to your 1st question:
Let's suppose there is an exception in your try block may be between these 2 lines
1.print("Hello")
2.print("Nothing went wrong")
In that case the second line will not be printed and the control will go to except block,so the output would not be the same.
The answers are same as there is no exception in your codes

So, when you have a code in which an exception might occur and you want something to get executed if that exception doesn't occur write it in else block.

Now answering to your 2nd question:
Finally is used when you want something to execute even if an exception occurs in try block.
Again your output are same because there is no exception in your try blocks.Try raising an exception and see the difference yourself.

Dharman
  • 30,962
  • 25
  • 85
  • 135