1
# phone num
    try:
        phone=soup.find("div", "phone-content")
        for a in phone:
            phone_result= str(a).get_text().strip().encode("utf-8")
        print "Phone information:", phone_result
    except ValueError:
        phone_result="Error"

My program stops when there is a uni-code error but i want to use try except to avoid terminating the program. how can i do it?

I get different types of errors. i want something which just doesnt terminate the loop no matter whatever the error. just i want to skip the part of the loop which has error

user3265370
  • 121
  • 1
  • 2
  • 12
  • well, what type of exception is the "uni-code error" you're trying to handle? – mhlester Feb 04 '14 at 05:02
  • put another way, what is the traceback when you run this and it doesn't work.. – mhlester Feb 04 '14 at 05:02
  • dup: http://stackoverflow.com/questions/4990718/python-about-catching-any-exception – zhangxaochen Feb 04 '14 at 05:05
  • Please understand that a robust program is not a program that is hard to kill, but one that keeps in control of what happens and handles edge situations correctly. Catching any error and continuing no matter what (without explicitly handling specific types and locations of error) is just an easy way to write **horrible** software. – 6502 Feb 04 '14 at 05:26

2 Answers2

1

By using

try:
    ...
except:
    ...

You could always catch every error in your try block attempt. The problem with THAT is that you'll catch even KeyboardInterrupt and SystemExit

However, it's better to catch Errors you're prepared to handle so that you don't hide bugs.

try:
    ...
except UnicodeError as e:
    handle(e)

At a minimum, you should at least be as specific as catching the StandardError.

Demonstrated using your code:

try:
    phone=soup.find("div", "phone-content")
    for a in phone:
        phone_result= str(a).get_text().strip().encode("utf-8")
    print "Phone information:", phone_result
except StandardError as e:
    phone_result="Error was {0}".format(e)

See the Exception Hierarchy to help you gauge the specificity of your error handling. http://docs.python.org/2/library/exceptions.html#exception-hierarchy

Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
  • Right, which I address, you're not giving me enough time here, man. :) – Russia Must Remove Putin Feb 04 '14 at 05:06
  • I get different types of errors. i want something which just doesnt terminate the loop no matter whatever the error. just i want to skip the part of the loop which has error – user3265370 Feb 04 '14 at 05:18
  • ok, Use StandardError, I'll demonstrate, see my usage of your code, and how I show you how to log the error in your data. – Russia Must Remove Putin Feb 04 '14 at 05:24
  • so it will just keep printing the error no matter what is it? and wont terminate the program right? – user3265370 Feb 04 '14 at 05:36
  • Well, you can still ctrl-C to stop it, because you don't want to block that, do you? It will cover anything you should run into in the builtin exceptions, while still giving you the functionality you need. StopIteration is for telling loops you're done with your iterator, you don't want to catch that. This is the everything but the kitchen sink exception handling, and seriously, don't catch the kitchen sink. See the Exception Hierarchy I linked at the end to see what all it catches. – Russia Must Remove Putin Feb 04 '14 at 05:41
0

I think you can try something like this first:

try:
    whatever()
except Exception as e:
    print e

Then you will get your desired(!) exception [if it already exists], and then change it to except somethingelse

Or you can go for custom exception which will look something like following, but you have raise it where necessary:

class ErrorWithCode(Exception):
    def __init__(self, code):
        self.code = code
    def __str__(self):
        return repr(self.code)
sadaf2605
  • 7,332
  • 8
  • 60
  • 103