What exception can be raised if the given line does not correspond to some format?

This is an earlier question.

Given the ISBN, for example. 3-528-03851-5what type of exception should be raised if the transmitted string does not match the format X-XXX-XXXXX-X?

+3
source share
3 answers

Lift a ValueError.

This is pretty much the standard way of saying, "You gave me a meaning that doesn't make sense." For instance:

>>> int ("a")
Traceback (most recent call last):
  File "", line 1, in 
ValueError: invalid literal for int () with base 10: 'a'
>>> import shlex; shlex.split ("'")
Traceback (most recent call last):
   ...
ValueError: No closing quotation

TypeError, , :

>>> d = {}
>>> d[{}]
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unhashable type: 'dict'
+6

, . ValueError:

class ISBNFormatException(ValueError):
    """Raised when an invalid ISBN format is found"""
    pass
+3

Source: https://habr.com/ru/post/1772014/


All Articles