Exception Handling for Python3

Source: Internet
Author: User

There are some exceptions when Python runs, but Python stops when it encounters an exception, but sometimes we need python to run down even when there are exceptions, and we need to use exception handling.
1. Simple exception handling such as:

print(a)Traceback (most recent call last):  File "<input>", line 2, in <module>NameError: name ‘a‘ is not defined

We do not define the variable a when it will be error, the error is Nameerror
How are we going to keep him running like this?
Use the exception handling on the line!

try:    print(a)except NameError:    print(‘NameError 意思就是没有这个变量啊‘)NameError 意思就是没有这个变量啊

So even if we don't define a variable, the program will continue to run down.

2. Handle multiple exceptions together:

try:    print(a)    lo        print(‘试一下异常之后的程序会不会被运行!‘)except (NameError,SyntaxError) as eor:    print(‘这个的报错是: %s‘%eor)这个的报错是: name ‘a‘ is not defined

There is no definition of a so it is nameerror, this exception handling is also in order, if there is a program after the exception will not be run

a=‘hello‘try:    print(a)    lo        print(‘试一下异常之后的程序会不会被运行!‘)except (NameError,SyntaxError) as eor:    print(‘这个的报错是: %s‘%eor)hello这个的报错是: name ‘lo‘ is not defined

Summary:
1. After the exception of the program will not be run;
2. The error message may be the same, but the type of error is not the same
If the error message is ' name ' lo ' isn't defined ' but one is Nameerror, one is SyntaxError

3. Common exception Handling
Sometimes we do not know the program will have any error, this time we need to use the Execption
This is all the error is its subclass, it includes all the error

try:    print(a)except Exception as err:    print(err)name ‘a‘ is not defined

4. If our program executes a statement without exception, use the Else

try:    print(‘a‘)except NameError as err:    print(‘这个是NameError语法错误!‘)except SyntaxError as err:    print(‘这个是SyntaxError语法错误!‘)else:    print(‘这个没错!‘)a这个没错!

5. In any case will be executed finally

try:    print(‘hello‘)except NameError:    print(‘这个是NameError!‘)except SyntaxError:    print(‘这个是SyntaxError!‘)finally:    print(‘我可不管是对是错我都会执行的!‘)hello我可不管是对是错我都会执行的!

6. Exceptions thrown in exception handling
The definition of this is that the program will be in the process of the exception that can occur, we treat the exception after processing (such as record), and then the exception is thrown out of the normal!

def exce(a,b):    try:        return a/b    except Exception as result:        print(‘我这里记录下了错误,错误是 --->%s‘%result)        print(‘我这里直接抛出了异常‘)        raiseexce(5,0)我这里记录下了错误,错误是 --->division by zeroTraceback (most recent call last):我这里直接抛出了异常  File "C:/Users/huxia/Desktop/py_script/28/temp.py", line 427, in <module>    exce(5,0)  File "C:/Users/huxia/Desktop/py_script/28/temp.py", line 422, in exce    return a/bZeroDivisionError: division by zero

If I don't throw an exception, I'll be fine.

def exce(a,b):    try:        return a/b    except Exception as result:        print(‘我这里记录下了错误,错误是 --->%s‘%result)        print(‘我这里直接抛出了异常‘)        #raiseexce(5,0)我这里记录下了错误,错误是 --->division by zero我这里直接抛出了异常

7. If sometimes we need to selectively throw exceptions that can be written like this:

class startE:    def __init__(self,choose):        self.choose = choose    def exce(self,a,b):        try:            return a/b        except Exception as result:            if self.choose:                print(‘我这里记录下了错误,错误是 --->%s‘%result)            else:                print(‘我这里直接抛出了异常‘)                raiseprint(‘我这里要打印错误日志‘)test = startE(True)test.exce(5,0)print(‘我不打印错误日志,我要抛出异常!‘)test = startE(False)test.exce(5,0)

So if we don't need to throw an exception, it's true, if we're going to throw an exception, it's false.

Exception Handling for Python3

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.