1

my code is like below

class Something(models.Model)

    def exception(self)
    try:
       Something.objects.all()
    except Exception():
       raise Exception()

called this method from testcases ,its working but i need to raise exception ,it does not catch the exception and here is my test case

def test_exception(self):
    instance = Something()
    instance.exception()

its working fine but i need to raise exception from except block

2
  • 2
    It is very unclear what you are trying to do. Why are you raising an exception from your catch-block? Also, there are numerous syntax-errors in your example (e.g. missing : and bad indentation on exception). Commented Oct 20, 2011 at 8:10
  • If you using the python unittest framework you probably wanna use the assertRaises(exception) method. Commented Oct 20, 2011 at 8:13

3 Answers 3

8

This line:

except Exception():

should be:

except Exception:
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, but of course it shouldn't be, as it's an extremely bad idea to just catch Exception - you should catch the actual exceptions you handle, and leave the others to bubble up.
May I know the reason of the downvote? My reply is technically correct and answers exactly the OP question, so I see no justifiable reason for a downvote, see StackOverflow's FAQ. If you want to advise the OP about good Python programming practices, comment the question.
@nicola You are right. Indeed I upvoted it. It answer the question better than the other answers, which point out the right use, but not why the original example is failing. I suspect it should be also raise Exception (without parenthesis) or raise. good practice vs bad practice was not part of the question.
2
def exception(self)
    try:
        Something.objects.all()
    except Exception, err:
        #print err.message (if you want)
        raise err

This will catch the error and print the exact msg if required.

1 Comment

I believe you meant raise err. You cannot raise err.message which is a string.
0

Why catch the Exception just to re-raise it? If you are not doing anything in the except suite except re-raising the exception, then simply do not catch the exception in the first place:

@staticmethod
def exception():
    Something.objects.all()

If you are doing something nontrivial inside the except suite, then:

def exception(self):
    try:
        Something.objects.all()
    except Exception:
        # do something (with self?)
        raise 

Then, to test that the exception method raises an Exception:

def test_exception(self):
    instance = Something()
    self.assertRaises(Exception, instance.exception)

This depends on Something.objects.all() raising Exception.


PS. If exception does not depend on self, then it is best to remove it from the argument list and make exception a staticmethod.

PPS. Exception is a very broad base exception class. A more specific exception would be more helpful for debugging, and allow other code to catch this specific exception instead of forcing it to handle any possible Exception.

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.