Error handling in Python

Source: Internet
Author: User
Tags assert

Error handling Syntax:

Try ... except ... finally ...

Put code that could be faulted into a try statement, execute an error, execute the EXCEPT statement block. If there is a finally statement block, then the finally statement block is executed. Execution is not wrong, the except statement block is not executed, but the finally statement block is bound to execute.

There can be several different except statement blocks.

Instance:

Try:print (' Try ... ') R = 10/int (' a ') print (' Result: ', R) except ValueError as E:print (' ValueError: ', E) except Zerodivisionerror as E:print (' Zerodivisionerror: ', e) finally:print (' finally ... ') print (' END ')

If there are no errors, you can add an else to the except, and do else automatically.

Try:print (' Try ... ') R = 10/int (' a ') print (' Result: ', R) except ValueError as E:print (' ValueError: ', E) except Zerodivisionerror as E:print (' Zerodivisionerror: ', e) else:print (' No error! ') Finally:print (' finally ... ') print (' END ')

All of Python's errors are inherited from Baseexception, and capturing errors can span multiple layers of calls.


Throw error

To throw an error, you first need to define an incorrect class, choose a good inheritance relationship, and then use the raise statement to throw an instance of the error.

Class Fooerror (ValueError): Pass def foo (s): n = Int (s) if n = = 0:raise Fooerror (' Invalid value:%s '% s) return 10/n foo (' 0 ')


Python Debug Method:

Direct print (), simple rough.

Assert assert

With print, you can use assert instead.

Example:

def foo (s): n = Int (s) assert n! = 0, ' n is zero! ' return 10/ndef main (): foo (' 0 ')

Assertion fails, assert throws Assertionerror:

Logging

Replacing an assert with a logging,logging does not throw an error and can be output to a file.

Import Logginglogging.basicconfig (level=logging.info) # level has debug,info, warning, errors = ' 0 ' n = Int (s) Logging.info (' n=%d '% n) print (10/n)










Error handling in Python

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.