Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Pyhon Learning Note 4: exception

Shulou Source: shulou.com Published: 2022-06-03 05:20:40 11月24日 Update

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

Tags: Processing common files logs tests notes learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Information OPPO Reno Linux MariaDB