Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
code copy from pep-3134
#1
I'm attempting to run the snippet of code in PEP-3134, but I'm not getting the errors stated.
The code relates to implicit exception handling. The expectation, according to the PEP, is:

Quote:The ultimate result will be an AttributeError due to the misspelling of clos, whose __context__ points to a NameError due to the misspelling of ex, whose __context__ points to an IOError due to the file being read-only, whose __context__ points to a ZeroDivisionError, whose __context__ attribute is None.

#!/usr/bin/python3

def main(filename):
  file = open(filename)       # oops, forgot the 'w'
    try:
      try:
        compute()
      except Exception, exc:
        log(file, exc)
    finally:
      file.clos()         # oops, misspelled 'close'

def compute():
  1/0

def log(file, exc):
  try:
    print >>file, exc       # oops, file is not writable
  except:
    display(exc)

def display(exc):
  print ex                    # oops, misspelled 'exc'

if __name__ == "__main()__":
  main('t.txt')
I get the following syntax error instead of what is expected:
Error:
File "./exception_chaining.py", line 5 try: ^ IndentationError: unexpected indent
Can anybody suggest why ?
Reply
#2
(Aug-26-2018, 02:59 PM)bluefrog Wrote: Can anybody suggest why ?
Indentation is wrong on web-site,so have to fix that.
Here also a fix so it work Python 3.6 -->.
def main(filename):
    file = open(filename)  # oops, forgot the 'w'
    try:        
        try:
            compute()
        except Exception as exc:
            log(file, exc)
    finally:
        file.clos()   # oops, misspelled 'close'

def compute():
    1/0

def log(file, exc):
    try:
        print >>file, exc # oops, file is not writable
    except:
        display(exc)

def display(exc):
    print(ex)   # oops, misspelled 'exc'

main('result.csv')
Output:
λ python 394.py Traceback (most recent call last): File "394.py", line 5, in main compute() File "394.py", line 12, in compute 1/0 ZeroDivisionError: division by zero During handling of the above exception, another exception occurred: Traceback (most recent call last): File "394.py", line 16, in log print >>file, exc # oops, file is not writable TypeError: unsupported operand type(s) for >>: 'builtin_function_or_method' and '_io.TextIOWrapper'. Did you mean "print(<message>, file=<output_stream>)"? During handling of the above exception, another exception occurred: Traceback (most recent call last): File "394.py", line 7, in main log(file, exc) File "394.py", line 18, in log display(exc) File "394.py", line 21, in display print(ex) # oops, misspelled 'exc' NameError: name 'ex' is not defined During handling of the above exception, another exception occurred: Traceback (most recent call last): File "394.py", line 23, in <module> main('result.csv') File "394.py", line 9, in main file.clos() # oops, misspelled 'close' AttributeError: '_io.TextIOWrapper' object has no attribute 'clos'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python code to copy data from multiple workbooks into master sheet Fatman003 2 3,765 Aug-21-2019, 11:23 AM
Last Post: Fatman003

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020