Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raise two errors at the same time

Tags:

python

Is there any way to raise two errors at the same time by using try and except? For example, ValueError and KeyError.

How do I do that?

like image 305
JuM Avatar asked Oct 10 '12 18:10

JuM


People also ask

Is it possible to raise more than one exception at a time?

Strictly speaking you can't raise multiple exceptions but you could raise an object that contains multiple exceptions.

How do you raise two exceptions in Python?

By handling multiple exceptions, a program can respond to different exceptions without terminating it. In Python, try-except blocks can be used to catch and respond to one or multiple exceptions. In cases where a process raises more than one possible exception, they can all be handled using a single except clause.

Can you test for more than one error in one except line?

Yes, it is possible to check multiple error types with a single try and except statement. For the except , we can include multiple error types listed within a tuple. It will check if any of the listed errors are encountered.

Can you have multiple excepts in Python?

Python Multiple ExceptsIt is possible to have multiple except blocks for one try block.


3 Answers

The question asks how to RAISE multiple errors not catch multiple errors.

Strictly speaking you can't raise multiple exceptions but you could raise an object that contains multiple exceptions.

raise Exception(
    [
        Exception("bad"),
        Exception("really bad"),
        Exception("really really bad"),
    ]
)

Question: Why would you ever want to do this?
Answer: In a loop when you want to raise an error but process the loop to completion.

For example when unit-testing with unittest2 you might want to raise an exception and keep processing then raise all of the errors at the end. This way you can see all of the errors at once.

def test_me(self):

    errors = []

    for modulation in self.modulations:
        logging.info('Testing modulation = {modulation}'.format(**locals()))

        self.digitalModulation().set('value', modulation)
        reply = self.getReply()

        try: 
            self._test_nodeValue(reply, self.digitalModulation())
        except Exception as e:
            errors.append(e)

    if errors:
        raise Exception(errors)
like image 93
shrewmouse Avatar answered Sep 28 '22 22:09

shrewmouse


You could raise an error which inherits from both ValueError and KeyError. It would get caught by a catch block for either.

class MyError(ValueError, KeyError):
    ...
like image 45
Collin Avatar answered Sep 28 '22 22:09

Collin


Yes, you can handle more than one error, either using

try:
    # your code here
except (ValueError, KeyError) as e:
    # catch it, the exception is accessable via the variable e

Or, directly add two "ways" of handling different errors:

try:
    # your code here
except ValueError as e:
    # catch it, the exception is accessable via the variable e
except KeyError as e:
    # catch it, the exception is accessable via the variable e

You may also leave out the "e" variable.

Checkout the documentation: http://docs.python.org/tutorial/errors.html#handling-exceptions

like image 45
soerface Avatar answered Sep 28 '22 21:09

soerface