codersofthedark
codersofthedark

Reputation: 9645

Exception returning no exception

I got a code something like this:

try:
     do_something()
except (urllib2.URLError, socket.timeout), er:
     print something 
except Exception, ex:
     print "The code failed because", ex, "Please review"

Now, the problem is on executing the above code I am getting following output:

The code failed because Please review

p.s.: The 'ex' should return the name of the exception but its not returning anything. Any idea why?

In reference to @Yuji and @Peter, I tried this code:

try:
    try:
           print x
    except Exception, ex:
           print "ex:", ex
           raise
except Exception, er:
    print "er:", er

And the Output was:

ex: name 'x' is not defined .
er: name 'x' is not defined .

Now, why raise(er) is returning an error? And why it does not in you cases?

Upvotes: 0

Views: 166

Answers (1)

勿绮语
勿绮语

Reputation: 9320

Not necessarily - the expectation is not entirely true. The following code prints nothing:

try:
    raise BaseException()
except BaseException, ex:
    print ex 

But this prints "abc":

try:
    raise BaseException("abc")
except BaseException, ex:
    print ex

Upvotes: 2

Related Questions