Pyhon Learning Note 4: exception
1. Exception handling of files: try except FileNotFoundError
Try:
Open ("abc.txt",'r')
Except FileNotFoundError:
Print ("abnormal!")
Exception try except NameError of 2.name
Try:
Print (aa)
Except NameError:
Print ("abnormal!")
3. You can use the BaseException class to handle all exceptions
Try:
Open ("abc.txt",'r')
Print (aa)
Except BaseException:
Print ("abnormal!")
4. Print exception log
Try:
# open ("abc.txt",'r')
Print (aa)
Except BaseException as msg:
Print ("abnormal!")
Print (msg)
Common exceptions in 5.python:
(1) try... else..
Try:
Aa= "exception testing"
Print (aa)
Except Exception as msg:
Print (msg)
Else:
Print ("No exception!")
(2) try... finally..
Try:
# open ("abc.txt",'r')
Print (aa)
Except BaseException as msg:
Print ("abnormal!")
Print (msg)
Finally:
Print ("I can execute it!")
6. Throw an exception