0

In the below code attached is there any way I can wait for else block print statement to get executed only after threading is done currently as the function gg is threaded I am getting output

HelloNothing went wrong

Hello

Expected output

Hello
Hello
Nothing went wrong

Current code

import threading
import time 


def gg():
    print("Hello")
    time.sleep(5)
    print("Hello")

try:
    threading.Thread(target=gg).start()
except:
    print("Something went wrong")
else:
    print("Nothing went wrong")
  • Does this answer your question? [Catch a thread's exception in the caller thread?](https://stackoverflow.com/questions/2829329/catch-a-threads-exception-in-the-caller-thread) – sudden_appearance Mar 04 '22 at 10:27
  • no i am not trying to catch exception here i m looking for execution of else statemet only when try is finished completly –  Mar 04 '22 at 10:30

1 Answers1

0

You can use Join method

import threading
import time


def gg():
    print("Hello")
    time.sleep(5)
    print("Hello")


t1 = threading.Thread(target=gg)
try:
    t1.start()
    t1.join()
except:
    print("Something went wrong")
else:
    print("Nothing went wrong")