Pass variable to an exception?
<p>I am trying to learn Python and I want to know if it is possible to pass a variable to an <code>Exception</code>? This is the code I have:</p>

<pre><code>try:
    staffId = int(row['staffId'])
    openingSalary = int(row['initialSalary'])
    monthsWorked = float(row['monthsWorked'])
except CutomException:
    pass

class CustomException(ValueError): # raised if data conversion fails
    def __init__(self):
        print("There was a problem converting data")
</code></pre>

<p>I want to pass <code>staffId</code> to the exception so that I can print something like: </p>

<p>print("There was a problem converting data for staff Id: ", staffId)</p>

<p>I tried this with no success: <a href="http://stackoverflow.com/questions/6626816/how-to-pass-a-variable-to-an-exception-when-raised-and-retrieve-it-when-excepted">How to pass a variable to an exception when raised and retrieve it when excepted?</a></p>

<p>The caller of the exception, e.g. the one that <code>raise</code> exception will have to pass an argument to the constructor.</p>

<pre><code>class CustomException(ValueError): # raised if data conversion fails
    def __init__(self, message):
        self.message = message;
        print("There was a problem converting data")


try:
    try:
        staffId = int(row['staffId'])
        openingSalary = int(row['initialSalary'])
        monthsWorked = float(row['monthsWorked'])
    except ValueError as e:
        raise CutomException(e);
except CutomException:
    pass
</code></pre>

python exception