Python, exception

Source: Internet
Author: User

Python, exception

Like Java, Pyton exception objects indicate exceptions. When an error occurs, an exception is thrown. If the exception object is not processed or captured, the program uses the so-called trace ack to terminate the execution;

>>> 1/0

Traceback (most recent call last ):

File" ", Line 1, in

ZeroDivisionError: Division by zero

The program can manually throw an exception through raise javastin.

>>>Raise Exception

Traceback (most recent call last ):

File" ", Line 1, in

Exception

>>>Raise Exception ("hello exception ")

Traceback (most recent call last ):

File" ", Line 1, in

Exception: hello exception

To customize the Exception type, you only need to inherit the Exception (or its subclass;

>>>Raise 1

Traceback (most recent call last ):

File" ", Line 1, in

TypeError:Exceptions must derive from BaseException

Unlike C ++, only exception classes can be thrown;

>>> Class MyException (Exception):

... Pass

...

>>>Raise MyException

Traceback (most recent call last ):

File" ", Line 1, in

_ Main _. MyException

Capture exceptions

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + dhj5o1_vcd4kpha + signature + OjwvcD4KPHA + oa08L3A + signature + ICA8L3A + signature + z + signature/Signature/ ojrL/logs + Pj4 + IHRyeTo8L3A + signature + ZWxzZbXEv + nU2sO709DS/Signature + Pj4 + signature + OjwvcD4KPHA + signature + Pj4 + IHRyeTo8L3A + CjxwPi4uLiAxLzA8L3A + signature "finally ')

...

Finally

Traceback (most recent call last ):

File" ", Line 2, in

ZeroDivisionError: division by zero

>>> Def f ():

... Try:

.... Return 1/0

... Finally:

... Return 2

...

>>> F ()

2

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.