Best Practices for Python Exceptions?

后端 未结 5 836
北恋
北恋 2021-01-29 20:04

What are the best practices for creating exceptions? I just saw this, and I don\'t know if I should be horrified, or like it. I read several times in books that exceptions shoul

5条回答
  •  面向向阳花
    2021-01-29 20:43

    I believe the advice against creating exceptions with a string comes from "Learning Python" (O'Reilly). In a section entitled String Exceptions Are Right Out!, it points out the (now removed) ability to create an exception directly with an arbitrary string.

    The code it gives as an example is:

    myexc = "My exception string"
    try:
        raise myexc
    except myexc:
        print ('caught')
    

    This is on p858 of the Fourth Edition (paperback).

提交回复
热议问题