Exception handling of Python basics

Source: Internet
Author: User

--Introduction

When the program runs, it is inevitable that the bug, when the bug, the user may be difficult to understand that the pile of error code is what is what, in order to allow users to better understand the cause of the errors or directly to the user to block this error, exception processing came into being.

--format

Try: #输入要运行的代码    pass except Nameerror as err: #将该类型的错误捕获    passexcept Exception as err: #将所有类型的错误捕获    passelse: #如果没有错误, run the code    passfinally: #不管是否存在错误, run the code    Pass

----Types of errors

Common Errors All errors

The exception class can only be used to handle the specified exception condition, and cannot be processed if a non-specified exception occurs. Of course, specifying an exception class can also capture its own subclasses.

Instance:

Try:    print (res)          #变量res未定义except nameerror as err:    print (' Error:%s '%err) except Indexerror as err:    Print (' Index err ') Finally:    print (' End ') #结果 error:name ' res ' is not definedend

In Python's exception, there is a universal exception: Exception, which can catch arbitrary exceptions, namely:

Code

In this way, exceptions that require special handling or alerting need to be defined and finally defined exception to ensure that the program runs correctly.

----advantages

Using try......except to catch errors is not only easy, but there is also a huge benefit of being able to cross multiple layers of calls.

def real (num):    return num/2def operate01 (num):    num=num+2    return Real (num) def operate02 (num):    try:        return operate01 (num)    except Exception as err:        print (' ERROR!! ') Print (OPERATE02 (' 2 ')) #结果出错了!! None

In this case, the actual error is in OPERATE01, and we can capture it directly in the OPERATE02.

----Proactively triggering exceptions

Try:    raise Exception (' Wrong!! ') except Exception as E:    print (e) #结果出错了!!

You can use the raise statement to throw an exception. The exception/Error object must have a name, and they should be subclasses of the error or exception class.

Class Invalidpage (Exception):    def __init__ (self,data):        self.data = datatry:    raise Invalidpage (' HAHA ') Except Invalidpage as a:    print ('%s is a Invalid page '%a.data)

Exception handling of Python basics

Related Article

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.