Sunday, 15 May 2011

python - Raising a custom message for exception in function -



python - Raising a custom message for exception in function -

mwe of code distribution:

main.py ../func1.py

from main.py phone call func1.py with:

data_list = [elem1, .., elemn] # info input. params = [1., 2., 5.] # parameters. elem in data_list: try: func1(elem, params) # phone call function. except exception: print traceback.format_exc()

this way if function fails element, main code keeps running executing remaining elements in list.

i want insert custom error message given block of func1, i've defined:

try: # seek except valueerror: raise valueerror('custom error message.')

when valueerror occurs in func1 output this, before jumping next element in data_list, is:

traceback (most recent phone call last): file "/main.py", line 44, in main func1(params) file "/func1.py", line 68, func1 raise valueerror('custom error message.') valueerror: custom error message.

why custom error message beingness printed twice?

the exception not raised twice. there can 1 exception "up in air".

what seeing whole traceback, extraordinary help when comes finding out why , programme crashed, prints line line frames , ends in line exception thrown. hence can read message "again".

if grab , print it, see exception itself. instance

>>> try: ... raise valueerror('custom error message.') ... except valueerror exc: ... print exc ... custom error message. >>>

python exception exception-handling

No comments:

Post a Comment