Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to combine value errors?

I have two function which check correctness of datas. Both function raise ValueError. Those funcions are executing in "for" loop so when one function will raise error, second will not be executed. Is there any possibility to combine both ValueErrors and return them via third function?
Ex:

datas = [4, "is", "data", None]

def func(word):
    if not word:
        raise ValueError("Wrong data!")

def func1(word):
    if type(data) != str:
        raise ValueError("Data isn't string!")

for data in datas:
    func(data)
    func1(data)

What I want as output:

ValueError: Wrong data!
ValueError: Data isn't string!

I’m not sure if it possible with ValueErrors but maybe there is some other way of combine errors?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The least hacky way to achieve this would be to accumulate the exceptions in a list, then handle the errors as you see fit:

datas = [4, "is", "data", None]

errors = []

def func(word):
    if not word:
        raise ValueError("Wrong data!")

def func1(word):
    if type(data) != str:
        raise ValueError("Data isn't string!")

for data in datas:
    try:
        func(data)
        func1(data)
    except ValueError as e:
        errors.append(e)

for error in errors:
     print(error)

will output

Data isn't string!
Wrong data!
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading