Python Exception Learning notes _python

Source: Internet
Author: User

An exception (exceptions) is a very important type in Python, which differs from a syntax error and is an error that is raised during the execution of a program. Python has a lot of exceptions built into it, such as Ioerror,nameerror,keyboardinterrupt, and more exceptions can be clicked here.

The meaning of the exception is to provide a more elegant way of running, such as writing a calculator in Python, and if the user enters an object that cannot be evaluated, it can throw an exception and process it as follows:

While True:
  try:
    x= int (input (' to enter A number: ')
    "print" Your input is%s "%x break
  except E Xception,e:
    Print E

Python is an object-oriented language, and the exception itself is an object, using Dir (Exception) to view the properties of the Exception class, as follows: [' __class__ ', ' __delattr__ ', ' __dict__ ', ' __doc__ ', ' _ _format__ ', ' __getattribute__ ', ' __getitem__ ', ' __getslice__ ', ' __hash__ ', ' __init__ ', ' __new__ ', ' __reduce__ ', ' __ Reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __setstate__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' __unicode__ ', ' Args ', ' message ', except args and message, all the rest are its internal properties, where args is a parameter of type Ganso passed to the constructor of the exception class, and some built-in functions such as ioerror need it to receive multiple parameters. The other is directly passing an error prompt string directly.

Python exceptions can be checked by a try statement, and any code in a try statement block is monitored to check for exceptions, except the type of the exception based on the input, and executes the code within the except. So here's the question, what are the two parameters behind the except? If the first is the wrong type, what about the second argument? To detect it, it is found to be an instance of exception, that is to say, it is a specific exception object generated by the exception class.
So, what if the user customizes an exception? Python stipulates that all exceptions must be inherited directly or indirectly from the exception class, such as the following, custom exceptions:

#!/usr/bin/env python
class Myerror (Exception):
  def __init__ (Self,*args):
    Self.value=args[0]
  def _ _str__ (self): return
    repr (self.value)
def showname (*args):
  if args:
    print args
  else:
    Raise Myerror (' error:need 1 arguments at last, 0 Input ')

By saving this file as showname.py, the other modules can introduce the call to the ShowName function and test the results of its execution:

#!/usr/bin/env python
import showname
try:
  showname.showname ()
except ShowName. Myerror,e:
  Print E

Notable points are: 1, Python throws an exception with raise; 2, because all exceptions are inherited from exception, so when the exception type is uncertain, you can catch all the exceptions directly after except, and 3, because of the exception inheritance, all the attributes within the exception can be redefined. You can also add attributes to a custom exception.

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.