Note that there are some explanatory texts on larger screens.

plurals
  1. POPython customized Exception class should allow to continue the program execution after it has been raised
    text
    copied!<p>I wrote a Python programm that has a customized Exception class called TAException and it works fine. But a new requirement forces me to extend its functionaliy. If the user sets a specific flag (-n) on program startup the program should NOT stop execution if TAException is raised.</p> <p>Below you see how I tried to implement it. In main() TAException.setNoAbort() gets called if that flag has been set. The rest is maybe self-explaining. The point is: Obviously it doesn't work. The program always aborts when TAException is raised. I know why it doesn't work, but I do not know how I can implement it differently. Could you please show me an elegant way to do this?</p> <pre><code>class TAException(Exception): _numOfException = 0 # How often has this exception been raised? _noAbort = False # By default we abort the test run if this exception has been raised. def __init__(self, TR_Inst, expr, msg): ''' Parameters: TR_Inst: Testreport instance expr: Expression in which the error occured. msg: Explanation for the error. ''' if TR_Inst != None: if TAException._noAbort is True: # If we abort the test run on the first exception being raised. TAException._numOfException += 1 # Or else only count the exception and continue the test run. # The status of the testreport will be set to "Failed" by TestReportgen. else: TR_Inst.genreport([expr, msg], False) # Generate testreport and exit. @staticmethod def setNoAbort(): ''' Sets TAException._noAbort to True. ''' TAException._noAbort = True </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload