Exception object of [Python]

Source: Internet
Author: User

First, the Exception object

class exceptions support the hierarchy of exceptions: The superclass becomes the class name, and the subclass becomes a particular kind of exception in this category. Except sentence lists a generic exception superclass that captures the various exceptions in the entire taxonomy: any particular subclass can be matched.

1 classGeneral (Exception):Pass2 classSpecific1 (General):Pass3 classSpecific2 (General):Pass4 5 defraise0 ():6x=General ()7     Raisex8 9 defraise1 ():Tenx=Specific1 () One     Raisex A  - defRaise2 (): -x=Specific2 the     Raisex -  -  forFuncinch(Raise0, Raise1, raise2): -     Try: + func () -     exceptGeneral : +         ImportSYS A         Print("Caught:", Sys.exc_info () [0]) at          -  forFuncinch(Raise0, Raise1, raise2): -     Try: - func () -     exceptGeneral as Ins: - #Import SYS in         Print("caught_as:"Ins.__class__) -  to #Output +Caught: <class '__main__. General'> -Caught: <class '__main__. Specific1'> theCaught: <class '__main__. Specific2'> *Caught_as: <class '__main__. General'> $Caught_as: <class '__main__. Specific1'>Panax NotoginsengCaught_as: <class '__main__. Specific2'>

Attention:

① must inherit a built-in exception superclass or built-in exception subclass, just like object:

Built-in exception class:

Baseexception: Exception top-level root class

Exception: Application-related exception top-level root superclass

Arithmeticerrot: Superclass of all numeric errors

Overfloeerror: A subclass that identifies a particular numeric error

② throwing an instance

In class exception mode, we always throw and capture a class instance object. An exception instance can be created before raise, just like the preceding example, or it can be created in the raise statement itself.

You can create an exception instance in the except ErrorName as instance.

③ Capture Classification

Try Statement

④ Exception Details

Access to exception details or exception states is primarily through exception instances.

Second, abnormal default printing and status

Built-in exceptions provide default print display and state hold, and any constructor arguments passed to these exception classes are saved in the instance's args tuple property, and are automatically displayed when the instance is printed (if no constructor arguments are passed, Arguments (shown as strings) are passed with an empty tuple and display string.

1 classE (Exception):Pass2 3 Try:4     RaiseE'spam','Temp')5 exceptE as x:6     Print(x)7 8 #Output9('spam','Temp')

Third, the exception object-Custom print display

By default, when an instance of a class-based exception is captured and printed, any content that we pass to the exception class constructor is displayed, and if custom displays or prints, the information for the default display (captured and printed) is the content in the custom __str__, and the passed parameters are accessed through the tuple attribute args.

When an exception is not caught, if the exception class instance is displayed as part of an error message, the inherited default display mode is used, and if the exception class is not customized, the passed constructor parameter is displayed, and if the exception class is customized, it is displayed as a custom content.

1 classMybad (Exception):Pass2 3 Try:4     RaiseMybad ('sorry! my mistake!')5 exceptMybad as x:6     Print(x)7     Print(X.args)8 Print('###'*8)9 Ten classBad (Exception): One     def __str__(Self, *args, * *Kwargs): A         return "Always look on the bright side for life ..." -  - Try: the     RaiseBad ('Bad a bad') - exceptBad as y: -     Print(y) -     Print(Y.args) + #Output - sorry! my mistake! +('sorry! my mistake!',) A ######################## at Always look on the bright side for life ... -('Bad a bad',)
1 classMybad (Exception):2     def __str__(self):3         return 'No catch exception'4 5 #Output6 Traceback (most recent):7File"F:\python\test.py", Line 265,inch<module>8     RaiseMybad ('default')9 __main__. Mybad: No catch exception
1 classMybad (Exception):Pass2 3 RaiseMybad ('default')4 5 #Output6 7 Traceback (most recent):8File"F:\python\test.py", Line 265,inch<module>9     RaiseMybad ('default')Ten __main__. Mybad: Default

Iv. Customizing data and Behavior

Similar and class inheritance, customization, extension;

An instance of the exception class was used (e.g. except Exception as instance)

Exception object of [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.