Python Advanced (24) – An Overview of Exceptions

Original link: https://chegva.com/5468.html

◎Knowledge points

  1. What is an exception?

  2. Built-in exception class object

◎Script practice

What is abnormal?

 """ What is an exception? An exception refers to a specific error that occurs during the execution of a program under the premise that there is no syntax error. Each specific error corresponds to an exception class object. When a specific error occurs, the instance object of the corresponding exception class object will be thrown. If the exception instance object thrown in the program is not caught and handled, the program will stop running and print the details of the error, including: (1) Traceback, which refers to the trace information of the exception call stack, which lists the number of relevant lines in the program (2) The name of the corresponding exception class object, and the error information of the exception. The exception instance object is caught and handled, and the program will continue to run  Which specific errors are considered exceptions? First, python has a lot of built-in exception class objects, and secondly, you can customize exception class objects, Therefore, errors corresponding to built-in exception class objects and custom exception class objects will be treated as exceptions. """  print(1 / 0) # ZeroDivisionError: division by zero  # print('123' + 456) # TypeError: can only concatenate str (not "int") to str  # print(int('abc')) # ValueError: invalid literal for int() with base 10: 'abc' 

Built-in exception class object

 """ Built-in exception class objects Python's built-in exception class objects and their inheritance relationships are as follows: BaseException ++-- SystemExit +-- KeyboardInterrupt ++-- GeneratorExit +-- Exception ++-- StopIteration ++-- StopAsyncIteration ++-- ArithmeticError | +-- FloatingPointError | +-- OverflowError | +-- ZeroDivisionError ++-- AssertionError ++-- AttributeError ++-- BufferError ++-- EOFError ++--ImportError | +-- ModuleNotFoundError ++-- LookupError | ++-- IndexError | +-- KeyError ++-- MemoryError +-- NameError | +-- UnboundLocalError ++-- OSError | +-- BlockingIOError | ++-- ChildProcessError | +/- ConnectionError | | +-- BrokenPipeError | | ++-- ConnectionAbortedError | | ++-- ConnectionRefusedError | | +-- ConnectionResetError | +-- FileExistsError | +-- FileNotFoundError | +-- InterruptedError | +-- IsADirectoryError | +-- NotADirectoryError | +-- PermissionError | +-- ProcessLookupError | ++-- TimeoutError ++--ReferenceError ++-- RuntimeError | ++-- NotImplementedError | +-- RecursionError ++-- SyntaxError | +-- IndentationError | +-- TabError ++-- SystemError ++-- TypeError ++-- ValueError | +-- UnicodeError | +-- UnicodeDecodeError | +-- UnicodeEncodeError | +-- UnicodeTranslateError +-- Warning ++-- DeprecationWarning ++-- PendingDeprecationWarning ++-- RuntimeWarning ++-- SyntaxWarning ++-- UserWarning ++-- FutureWarning ++-- ImportWarning +-- UnicodeWarning ++-- BytesWarning ++-- ResourceWarning   For the specific error represented by each exception class object, you can refer to the official documentation: https://docs.python.org/3/library/exceptions.html   The base class for all built-in exception class objects is Exception """ 

◎Script address: https://github.com/anzhihe/learning/blob/master/python/practise/learn-python/python_advanced/exception_def.py

This article is reprinted from: https://chegva.com/5468.html
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment