简体   繁体   English

哪个异常类用于文件解析错误?

[英]What exception class to use for file parsing error?

I'm writing a parser for a certain file format. 我正在为某种文件格式编写解析器。 If a file is not correctly formatted (and can not be parsed) then the parser throws an exception. 如果文件格式不正确(并且无法解析),则解析器将引发异常。

What exception class, in the Python 2 exception hierarchy, should I use? 我应该使用Python 2异常层次结构中的哪个异常类?

How about 怎么样

class XyzParseError(Exception):
    pass

where XyzParser is the name of your parser class? XyzParserXyzParser是您的解析器类的名称? That's also what HTMLParser in the standard library does. 这也是标准库中的HTMLParser所做的。

ValueError似乎是适当的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM