Recently I came across a strange part of code in one of the repositories I use for work. In this part, an exception is raised with a boolean value:
if current_run_row_count == 0:
raise InconsistentData(
"0 rows found", False,
)
InconsistentData
inherits from Exception
class:
class InconsistentData(Exception):
pass
I do not understand what this False
corresponding to the exception raise does.
First my guess was that it is some marker whether this exception is actually a warning (I think I might have heard something like this). If it is True
, then exception stops program execution, and if it is False
, it does not stop it and acts as a warning. It could also be vice versa, but the idea is the same. However, I could not find any information about Exception
arguments in Python documentation - for instance, here and here.
Secondly, I thought that maybe this False
just does nothing and appears to be a part of the error message. I tested it with two different samples of code.
First one:
if 1 == 1:
raise Exception("error", False)
print('Hello!')
Output:
Exception: ('error', False)
Second one:
if 1 == 1:
raise Exception("error", True)
print('Hello!')
Output:
Exception: ('error', True)
So, in both code samples the exception is raised and stops program execution, and this boolean value seems to be a part of the error message. However, according to the code, I have no idea why would anyone include it in the error message. Along with InconsistentData
, there are two other custom exceptions: CustomException
and ValidationError
, and when they are raised, they do not have any boolean values, only an error message represented with text.
CodePudding user response:
It is probably second option - "this False just does nothing and appears to be a part of the error message". I will accept @deceze answer:
It means whatever the author wants it to mean. It has no influence on the exception as such, as you have correctly identified. It's just additional data.