0

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?

8
  • 1
    Maybe something just did a raise Exception? Commented Nov 23, 2011 at 5:01
  • @Yuji: can you please elaborate? May be with an example where raise at somewhere causes exception returning nothing. Commented Nov 23, 2011 at 5:13
  • 1
    @dragonsrsupercool for example, printing the exception message for raise Exception prints nothing. printing the exception for raise Exception('foo') or raise Exception, 'foo' prints "foo" Commented Nov 23, 2011 at 5:22
  • @Yuji: I have added a code block above, can you please review and tell why raise is returning error in my case but not in yours? Commented Nov 23, 2011 at 5:36
  • 1
    @dragonsrsupercool - you are not raising an exception, python is raising an exception because x is not defined, and the exception message for that NameError is "name 'x' is not defined". Update: the raise in the outer block is re-raising the first exception as it's supposed to Commented Nov 23, 2011 at 5:52

1 Answer 1

2

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 
Sign up to request clarification or add additional context in comments.

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.