I have this function. My pygame's text to rectangle converter.
def text_to_rect(text, name='default'):
try:
font = load.text_style[name]['font']
aa = load.text_style[name]['aa']
color = load.text_style[name]['color']
except NameError:
font_path = pygame.font.get_default_font()
font = pygame.font.Font(font_path, 24)
aa = 1
color = (0,0,0)
if not name=='default':
text = text+'(ERROR: Global load object not defined.)'
except KeyError:
font_path = pygame.font.get_default_font()
font = pygame.font.Font(font_path, 24)
aa = 1
color = (0,0,0)
if not name=='default':
text = text+'(ERROR: '+name+' text style does not exist.)'
return font.render(text,aa,color)
In two except blocks there are 4 lines of the same code. I want to run these 4 lines if any exception occurs, then rest to a specific exception.
You can actually combine exceptions into one statement:
try:
#code that you expect errors from
except KeyError, NameError:
#exception code
except:
#Of course, you can also do a naked except to catch all
#exceptions,
#But if you're forced to do this, you're probably
#doing something wrong. This is bad coding style.
EDIT
For your case, if you want your code execution to depend on the error caught, do this:
try:
#Code to try
except (KeyError, NameError) as e:
#Code to execute in either case
if isinstance(e, KeyError):
#code to execute if error is KeyError
else:
#code to execute if error is NameError
Related
Code:
genders=[]
for image in os.listdir('Face'):
try:
gender = int(image.split('_')[1])
except ValueError:
pass
genders.append(gender)
Trying to add int values of string in list.
Raises Value error
ValueError: invalid literal for int() with base 10: ''
so for example : imageName_1 get that one and add to a list. but sometimes after _ there is no number. so i want to catch that image and delete it but don't want to stop the iteration.
Maybe a small change in the code will work
genders=[]
for image in os.listdir('Face'):
try:
gender = int(image.split('_')[1])
genders.append(gender)
except ValueError:
pass
Using continue statement will allow you to continue iteration.
genders=[]
for image in os.listdir('Face'):
try:
gender = int(image.split('_')[1])
except ValueError:
continue
genders.append(gender)
import os
genders=[]
for image in os.listdir('Face'):
try:
genders.append(int(image.split('_')[1]))
except (ValueError, IndexError):
try:
os.remove(image)
except OSError:
pass
gender is defined in the try clause only, so you can't append it to genders, and you don't need to (because you want to delete the file), so this line should be in the try clause also:
genders=[]
for image in os.listdir('Face'):
try:
gender = int(image.split('_')[1])
genders.append(gender)
except ValueError:
# delete file
os.remove(image)
I have a block of code where exception is happening for some records while executing an imported method (pls see below). My goal is NOT to stop execution, but perhaps print (to understand what's wrong with the data) some values when in error and continue. I tried various way to use "try...except", but no luck! Can someone pls take a look and suggest? Many thanks in advance!
code below
if student_name not in original_df_names_flat:
for org in orgs:
data = calculate(org, data)
result = imported_module.execute(data) # here's the line where exception happens
return result
else:
return data
if student_name not in original_df_names_flat:
for org in orgs:
data = calculate(org, data)
result = None # May fail to get assigned if error in imported_module
try:
result = imported_module.execute(data) # here's the line where exception happens
except Exception as e:
print(f'{e}') # probably better to log the exception
return result
else:
return data
I guess an exception can occur in 2 places. So try this. It covers your exception line and the other possibility. So your program should keep running.
Secondly I have adjusted the indentation of the second try clause so the loop processes all data in orgs. To return all results I have added a list 'results'.
if student_name not in original_df_names_flat:
results = []
for org in orgs:
try:
data = calculate(org, data)
except Exception as e:
print(str(e))
try:
result = imported_module.execute(data) # here's the line where exception happens
results.append(result)
except Exception as e:
print(str(e))
return results
else:
return data
the below solution took care of the problem:
if student_name not in original_df_names_flat:
for org in orgs:
data = calculate(org, data)
try:
result = imported_module.execute(data) # here's the line where exception happens
except exception as e:
print ("exception happened", e)
pass
return data
return result
else:
return data
the above solution took care of the problem
Program description:
Program accepts a file name and a segment, consisting of two numbers (each divided by one space). Then it outputs all lines from existing file where their indicies lie within the given segment.
My solution:
import sys
try:
par = input("Input (<file> <low border of the segment> <high border of the segment>): ").split(' ')
print(17 * '-')
f = par[0]
f_lines = [line.strip("\n") for line in f if line != "\n"]
length = len(f_lines)
if (par == ''):
raise RuntimeError('Error: undefined')
if (par[2] == None) or (par[2] == ''):
raise RuntimeError('Error: segment is limited')
if ((par[1] and par[2]) == None) or ((par[1] and par[2]) == ''):
raise RuntimeError('Error: segment undefined')
if (int(par[2]) >= length):
raise RuntimeError('Error: segment can not be greater than length the amount of lines')
if (par[1] == ''):
a = 0
if (par[2] == ''):
b = 0
segment = [int(par[1]), (int(par[2]) + 1)]
with open(par[0]) as f:
data = f.read().splitlines()
for k in range(segment[0], segment[1]):
print(data[k])
except (FileNotFoundError, IOError, NameError):
print('[!] Error: your input is incorrect. The file may be missing or something else. \n[?] For further information see full error logs: \n',sys.exc_info())
except RuntimeError as e:
print(e)
Problem:
When I try running my program in different ways to test each of my Runtime errors I always get this message:
Traceback (most recent call last):
File "C:\Users\1\Desktop\IT\pycharm\sem_2.py", line 10, in <module>
if (par[2] == None) or (par[2] == ''):
IndexError: list index out of range
I cannot wrap my head around how can I properly handle multiple Runtime errors so they would display as a text message. I haven't found any solution to my question anywhere online, so I'm trying to ask here.
I will appreciate any help, thanks in advance.
Your code would catch FileNotFoundError, IOError, NameError and RuntimeError but what is actually thrown is IndexError and that is not handled.
You may want to add IndexError it to the first except block:
except (FileNotFoundError, IOError, NameError, IndexError):
print('[!] Error: input incorrect!') # ^^^^^^^^^^^^
or perhaps add another except block if you want a custom message for IndexError:
except (FileNotFoundError, IOError, NameError):
print('[!] Error: input incorrect!')
except IndexError:
print('[!] Error: IndexError just happened!')
By the way, the following will always be False because the code in parentheses will resolve to a bool first, which can either be True or False and these are obviously never equal to '' or None:
((par[1] and par[2]) == None) or ((par[1] and par[2]) == '')
You may way want to rewrite it to:
(par[1] is None and par[2] is None) or (par[1] == '' and par[2] == '')
In my code I have a main function, which calls a function that reads some data from a file and returns this data, which is then used in diffrent ways. Obviously there is a risk that the user inputs a filename that is not to be found, resulting in an error. I want to catch this error and output a error message written by me without the traceback etc. I tried using a standard try-except statement, which works almost as intended, except now the data is not read so there are new errors as I try to calculate using empty variabels. Using sys.exit or raise SystemExit in the exception block results in errors beeig written in the console with tracebacks and the whole point of catching the first error feels redundant. I could wrap the whole program in a try-statement, but I have never seen that being done and it feels wrong. How can I either terminate the program in a clean way or hide all the subsequent errors?
def getData(fileName):
try:
file = open(fileName,"r")
data = file.readlines()
file.close()
x = []
y = []
for i in data:
noNewline = i.rstrip('\n')
x.append(float(noNewline.split("\t")[0]))
y.append(float(noNewline.split("\t")[1]))
return x,y
except FileNotFoundError:
print("Some error messages")
def main(fileName):
x,y = getData(fileName)
# diffrent calculations with x and y
Because main is a function, you could return on an error:
def main(filename):
try:
x, y = getData(filename)
except FileNotFoundError:
print("file not found")
return
# calculations here
Solution
sys.exit and SystemExit take optional arguments—0 is considered a successful termination.
Example
sys.exit(0)
raise SystemExit(0)
References
Python sys.exit: https://docs.python.org/3/library/sys.html#sys.exit
below
def getData(fileName):
file = open(fileName,"r")
data = file.readlines()
file.close()
x = []
y = []
for i in data:
noNewline = i.rstrip('\n')
x.append(float(noNewline.split("\t")[0]))
y.append(float(noNewline.split("\t")[1]))
return x,y
def main(fileName):
# if you only want to handle exception coming from 'getData'
try:
x,y = getData(fileName)
except Exception as e:
print(f'could not get data using file {filename}. Reason: {str(e)}')
return
# do something with x,y
if __name__ == "__main__":
main('get_the_file_name_from_somewhere.txt')
This question already has an answer here:
Why do I get a `NameError` (or `UnboundLocalError`) from using a named exception after the `except` block?
(1 answer)
Closed 17 days ago.
I've looked at other questions regarding this particular error but in those cases there was a route in the process where the variable is never defined and hence fails when called later. However with my program I set the variables before the try method, have those variables changed in the try method under certain criteria, and then an if statement calls them later but fails. When Python reaches the if statement and tries to create the variable text, it claims the variables error and error1 are undefined (referenced before assignment).
err = 0
error = 'No Error Set'
error1 = 'No Error1 Set'
try:
conn = sqlite3.connect(db_dir)
for row in conn.execute("select max(date) from eod"):
mxdate = row
if mxdate == None:
pass
else:
for row in conn.execute('select date, status from eod where date = ?',(mxdate,)):
_lst_eod = row
for row in conn.execute("select * from temp"):
_crnt_eod = row
conn.close()
except Exception as error:
error = str(error)
err = 1
logged = 0
try:
conn.close()
time = str(right_datetime())[11:19].replace(':','')
conn = sqlite3.connect(db_dir)
conn.execute("insert into error_log (date, user, auth, error, _action) values (?,?,?,?,'Failed to select from eod/temp tables.')",(int(str(_date)+time),cred[0],cred[1],error,))
conn.commit()
conn.close()
logged = 1
except Exception as error1:
error1 = str(error1)
if err == 1:
#An error occured here.
text = '##Error## An error occured while trying to end the day:\n'+error
if logged == 0:
text = text+'\n\nA row was written to the error log.'
else:
text = text+'\n\nWrite to Error Log failed due to error:\n'+error1
else:
....carry on with the rest of the program.
In Python 3, the variable to which you assign the exception in a except SomeException as e: statement is deleted on exiting the except block:
>>> e = "something"
>>> try:
... 1/0
... except ZeroDivisionError as e:
... print(e)
...
division by zero
>>> e
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'e' is not defined
The same thing's happening in your code: the except Exception as error: and except Exception as error1: blocks are deleting error and error1 when they exit.