Python3 Exception Handling

Source: Internet
Author: User

What is an exception

Python uses exception objects (Exception object) to represent exception conditions. An exception is thrown when an error is encountered, and if the exception object is not processed or snapped , the program terminates execution with backtracking (traceback) .

Raise statement:

Exceptions can be raised automatically when something goes wrong. There are two ways to throw an exception:

1 . Using a class, it should be a subclass of Excepion (when using a class, the program automatically creates an instance of the class) 2. The instance parameter invokes the raise statement.
Instance (using the built-in exception exception class):
In [1]: raise Exception ---------------------------------------------------------------------------Exception Traceback (most recent call Last)<ipython-input-1-fca2ab0ca76b>inch<module>()---->1raise Exceptionexception: #raise Exception throws a common exception that does not have any information about the error. in [2]:Raise Exception (' The hyperdrive overload ')---------------------------------------------------------------------------Exception Traceback (most recent call Last)<ipython-input-2-3d981203beac>inch<module>()---->1Raise Exception ('The hyperdrive overload') Exception:the hyperdrive overload
#添加了错误信息
Built-in exception class:

There are many built-in exception classes, all in the exceptions module. The official website Manual (3.5.1 version), in 3.5.1+, is integrated into the built-in modules.

Id Class name Describe
1 Raise Exception base class for all exceptions
2 Raise Attributeerror Thrown when a feature reference or assignment fails
3 Raise IOError Trying to hit a nonexistent file (including other cases)
4 Raise Indexerror Use an index that does not exist in the sequence
5 Raise Keyerror Use a key that does not exist in the map
6 Raise Nameerror Name not found (variable)
7 Raise SyntaxError Thrown when the code is in the wrong form
8 Raise TypeError When a built-in operation or function is applied to an object of the wrong type
9 Raise ValueError The correct type object, but the object uses an inappropriate value
10 Raise Zerodivisionerror Division, the second parameter is 0
Custom Exception classes:

Make sure that you inherit from the exception class, either directly or indirectly (can be a subclass of other built-in exception classes), and write a custom exception class as follows:

#!/usr/bin/env python#Encoding:utf-8"""@author: Kamil.liu @file: Exception.py@time:16-5-30 morning 10:56"""#Raise Exception#Raise Attributeerror#Raise IOError#Raise Indexerror#Raise Keyerror#Raise Nameerror#Raise SyntaxError#Raise TypeError#Raise ValueError#Raise Zerodivisionerrorclasskamilexception (Exception):PassPrint(Type (kamilexception))Print(dir (kamilexception))

Results:

/usr/bin/python3.5/home/kamil/pycharmprojects/untitled/test1/exception.py<class 'type'>['__cause__','__class__','__context__','__delattr__','__dict__','__dir__','__doc__','__eq__','__format__','__ge__','__getattribute__','__gt__','__hash__','__init__','__le__','__lt__','__module__','__ne__','__new__','__reduce__','__reduce_ex__','__repr__','__setattr__','__setstate__','__sizeof__','__str__','__subclasshook__','__suppress_context__','__traceback__','__weakref__','args','With_traceback']process finished with exit code 0

Python3 Exception Handling

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.