Advertisement
#Python try-except with else statement
file = None
try:
file = open('D:\File1.txt','w'); #Opening a file to perform write operation
file.write('Helloooo from Python') #writing data to a file
file.flush() #flushes the data to a file
file.close() #Closing the file
except Exception as e :
print("An exception is caught :", e)
else:
print("Code didn't throw any exception")
Code didn't throw any exception
#Python try-except with else statement
file = None
try:
file = open('D:\File2.txt','w'); #Opening a file to perform write operation
file.write('Helloooo from Python') #writing data to a file
file.flush() #flushes the data to a file
#file.close()
else:
print("Code didn't throw any exception")
except Exception as e :
print("An exception is caught :", e)
#Python- An else block with try-except blocks, followed by a finally block example
try:
print("Hello from DecodeJava")
except Exception as e:
print( "Exception caught : ", e)
else:
print("Code didn't throw any exception")
finally:
print("A finally block executes with or without exception")
Hello from DecodeJava
Code didn't throw any exception
A finally block executes with or without exception
Advertisement
#Python- An else block following a finally block example
try:
print("Hello from DecodeJava")
except Exception as e:
print( "Exception caught : ", e)
finally:
print("A finally block executes with or without exception")
else:
print("Code didn't throw any exception")
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement