【ICS_32A】Lecture2Week3

Exception

try:
    the_file= file_path.open("r")
    line_count=0
    for line in the_file:
        line_count += 1
        return line_count
except OSError:
    print("That file should not be opened")
except ValueError:
    print("That file did not contain text")
finally:
    if the_file!=None:
        the_file.close()
  1. finally无论try有无出现问题,都会执行
  2. except下只会在try中出现问题时执行
  3. except (Exception):
  4. assert(Boolean Experssion)
    • 如果True,就执行下一行,如果False,就会停止并traceback一个"AssertionError"
    • 如果表达式内本来就会出bug,那么就会返回bug信息,而不会有AssertionError
  5. nested list of interger有且只有int和nested list of interger