Python: difference between ValueError and Exception?

前端 未结 2 489
忘了有多久
忘了有多久 2021-01-02 00:45

I am trying to understand what is a difference between raising a ValueError and an Exception. I have tried both in the same code (even in the same branch) and the result was

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-02 01:14

    You said it, ValueError is a specific Exception. A short example :

    try:
        print int("hello world")
    except ValueError:
        print "A short description for ValueError"
    

    If you change "hello world" with an int, print int(42), you will not raise the exception.

    You can see doc about exceptions here.

提交回复
热议问题