0

I have a statement like this.

I just want to know which of the two assert statement has thrown the exception.

try:
    assert re.search("xyz", statement)
    assert re.search("abc", statement)
except AssertionError:
    print "AssertionError : Expected Error message not found"

Thanks for the answer.

3
  • If you want to distinguish between the two, use separate try/except blocks for each statement. Commented Feb 2, 2017 at 7:10
  • 2
    The most simple solution is to remove the try / expect. Commented Feb 2, 2017 at 7:30
  • @KlausD. Fair comment, but perhaps Logesh wishes to continue execution after any AssertionErrors in that block are raised; of course, that's generally not a safe strategy, but it's sometimes useful, as in my demo. Commented Feb 2, 2017 at 7:37

4 Answers 4

1

As mentioned in The assert statement docs, you can give an expression after the assertion test expression; that second expression will be passed in the AssertionError. Here's a simple demo:

for n in (-5, 10, 20):
    try:
        assert 0 <= n, '%d is too low' % n
        assert n <= 10, '%d is too high' % n
        print('%d is ok' % n)
    except AssertionError as err:
        print "AssertionError:", err

output

AssertionError: -5 is too low                                                                                                                  
10 is ok                                                                                                                                       
AssertionError: 20 is too high     

That second expression doesn't have to be a string, it can be anything. Since assertions should only be used to verify program logic, not to validate user data, I generally don't bother passing a nicely-formatted string, I just pass a tuple containing the relevant values, and maybe an identifying string. Eg,

assert (a * b > c), ('Bad product', a, b, c)
Sign up to request clarification or add additional context in comments.

1 Comment

traceback library in python provides many utilities to extract more information of the raised exception
1

You could use functions from the traceback module. For example, extract_tb returns a list of tuples (named tuples in Python 3.5 and newer) representing the stack trace entries. Each tuple contains a line number as well as the source text line (if available).

import traceback

try:
    assert 1
    assert None
except AssertionError as e:
    for x in traceback.extract_tb(e.__traceback__, limit=-1):
        print(x.lineno, repr(x.line)) # Prints 5 'assert None'

Comments

0

You are able to print the last raised exception with traceback.print_exc(). An example:

>>> import traceback
>>> try:
...    a = 1 / 0
... except:
...    traceback.print_exc()
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ZeroDivisionError: integer division or modulo by zero

There is also traceback.format_exc() in case you don't want to print.

Comments

0

Maybe you want to differ the exceptions?Then Getting exception details in Python can help you. Copying the answer here:

import sys
try:
   assert re.search("xyz", statement)
   assert re.search("abc", statement)
except  AssertionError:
   type, value, traceback = sys.exc_info()

Then you can print out the info.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.