Python learning-Exception Handling, python-Exception Handling

Source: Internet
Author: User

Python learning-Exception Handling, python-Exception Handling

The Exceptions in Python are the base classes of all exceptions. The built-in exception classes are placed in the Exceptions module. You can see these built-in classes through the dir () function.

Use raise statements to trigger exceptions, as shown in figure

>>> Raise Exception

>>>import exceptions
>>>dir(exceptions)['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'EnvironmentError', 'Exception', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '__doc__', '__name__', '__package__']

When the code encounters an error, it will spread up layer by layer. If no exception capture mechanism is found, the error will be transmitted to the location of the global variable, causing the program to end with an error.

In python, try/try t can be used to catch exceptions. try t can catch up with built-in exception classes or custom exception classes.

1 import exceptions2 try: 3 print a4 partition t NameError: 5 print 'undefined name'View Code can also capture multiple exceptions and use tuples to list them. In addition, if you want to access the exception object itself, add a parameter to the compile t statement, such as import exceptionstry: print aworkflow t NameError, e: print eView Code

Note: In python 3.0, the alias t statement is written as execpt NameError as e:

Other statements:

Else statement: else runs only when no exception is found.

Finally statement: all statements in exception handling are executed.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.