Skip to main content

PYTHON TRY...EXCEPT

T
RY...EXCEPT
try:
  f = open("demofile.txt")
  try:
    f.write("Lorum Ipsum")
  except:
    print("Something went wrong when writing to the file")
  finally:
    f.close()
except:
  print("Something went wrong when opening the file")
Something went wrong when writing to the file
x = "hello"
if not type(x) is int:
  raise TypeError("Only integers are allowed")
Traceback (most recent call last):
  File "/usr/lib/python3.8/idlelib/run.py", line 559, in runcode
    exec(code, self.locals)
  File "/home/kartik/Downloads/tryCatch.py", line 16, in <module>
    raise TypeError("Only integers are allowed")
TypeError: Only integers are allowed

Comments