Python Try Except stopping program as opposed to skipping over errror
Question:
I am running a sequence of scripts that are independent of each other. Meaning if one of them fails I want the rest to continue on and just be notified of the one that failed(I can implement the notification later). For now I am just trying to figure out why the program is not continuing on.
I am trying doing a test in which I have the sequence. However Script 2 in this case has an error as there is not a file its trying to read in. I am getting
FileNotFoundError: [Errno 2] No such file or directory:
And the program is stopping.
def errorFunction(function, name):
print('Running Script: ' + name)
try:
function
except FileNotFoundError:
print('FileNotFound Error. Script Name ' + name)
except:
print('Error. Script Name ' + name)
pass
errorFunction(Script1(), 'Script1')
errorFunction(Script2(), 'Script2')
errorFunction(Script3(), 'Script3')
Answers:
errorFunction(Script1(), 'Script1')
This is your problem. Since there are parentheses on the end of Script1()
, you are actually calling that function here, before errorFunction()
even executes.
Take off the parentheses here, like so:
errorFunction(Script1, 'Script1')
And then add parentheses here, like so:
try:
function()
I am running a sequence of scripts that are independent of each other. Meaning if one of them fails I want the rest to continue on and just be notified of the one that failed(I can implement the notification later). For now I am just trying to figure out why the program is not continuing on.
I am trying doing a test in which I have the sequence. However Script 2 in this case has an error as there is not a file its trying to read in. I am getting
FileNotFoundError: [Errno 2] No such file or directory:
And the program is stopping.
def errorFunction(function, name):
print('Running Script: ' + name)
try:
function
except FileNotFoundError:
print('FileNotFound Error. Script Name ' + name)
except:
print('Error. Script Name ' + name)
pass
errorFunction(Script1(), 'Script1')
errorFunction(Script2(), 'Script2')
errorFunction(Script3(), 'Script3')
errorFunction(Script1(), 'Script1')
This is your problem. Since there are parentheses on the end of Script1()
, you are actually calling that function here, before errorFunction()
even executes.
Take off the parentheses here, like so:
errorFunction(Script1, 'Script1')
And then add parentheses here, like so:
try:
function()