raise or not to raise [Newbie]

Discussion in 'Python' started by Jacol, Feb 3, 2007.

  1. Jacol

    Jacol Guest

    Hello everybody!

    I found this:
    http://mail.python.org/pipermail/python-list/1999-July/006344.html

    My question concernings "callerFunc" and is more general what is difference
    between:

    try:
    raise "xyz"
    except "xyz":
    print "This is smthing"

    and simpler:
    print "This is smthing"

    Both give the same result.
    I don't understand manuals in this point.

    Thanks a lot,
    Jacek
     
    Jacol, Feb 3, 2007
    #1
    1. Advertising

  2. Jacol

    Jacol Guest

    I understand that author generated exception and than extracted the name of
    function from the exeption. But is any sens in using exeptions service if
    we have smthing simpler: just print for example? In my opinion no, it
    doesn't make sens.

    Jacek
     
    Jacol, Feb 3, 2007
    #2
    1. Advertising

  3. Jacol

    Paddy Guest

    On Feb 3, 9:07 pm, Jacol <> wrote:
    > I understand that author generated exception and than extracted the name of
    > function from the exeption. But is any sens in using exeptions service if
    > we have smthing simpler: just print for example? In my opinion no, it
    > doesn't make sens.
    >
    > Jacek


    You can terminate your program by raising an exception that you don't
    otherwise catch and handle. e.g:

    >>> def with_error():

    .... print "Print this then raise an error"
    .... raise Exception("Bye Bye")
    ....
    >>>
    >>> with_error()

    Print this then raise an error
    Traceback (most recent call last):
    File "<interactive input>", line 1, in <module>
    File "<interactive input>", line 3, in with_error
    Exception: Bye Bye
    >>>


    Notice how the traceback, automatically added to un-caught exceptions,
    shows were it was raised.

    Your link points to a very old version of Python and error handling
    has changed. Please use a more recent tutorial such as THE Python
    tutorial here:
    http://docs.python.org/tut/node10.html

    - Paddy.
     
    Paddy, Feb 3, 2007
    #3
  4. Jacol

    Robert Kern Guest

    Jacol wrote:
    > I understand that author generated exception and than extracted the name of
    > function from the exeption. But is any sens in using exeptions service if
    > we have smthing simpler: just print for example? In my opinion no, it
    > doesn't make sens.


    You are correct. The author of that code was using exceptions to get at
    particular information that, at the time, was only available through a traceback.

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
     
    Robert Kern, Feb 3, 2007
    #4
  5. Jacol a écrit :
    > I understand that author generated exception and than extracted the name of
    > function from the exeption. But is any sens in using exeptions service if
    > we have smthing simpler: just print for example? In my opinion no, it
    > doesn't make sens.


    You're of course right. Exceptions are mainly a way of handling
    'exceptional' conditions without cluttering the source code with error
    code checking. The canonical use case is:

    try:
    some_call_that_may_raise(args)
    except SomeException, e:
    try_to_solve_the_problem()

    The nice thing with exceptions (compared to 'manual' error handling) is
    that you can choose where you want to handle the problem without having
    to pass back error code/error message all along the call stack...
     
    Bruno Desthuilliers, Feb 4, 2007
    #5
  6. En Sat, 03 Feb 2007 18:07:12 -0300, Jacol <>
    escribió:

    > I understand that author generated exception and than extracted the name
    > of
    > function from the exeption. But is any sens in using exeptions service if
    > we have smthing simpler: just print for example? In my opinion no, it
    > doesn't make sens.


    Notice the posting date... 8 years ago, that was the only way. Now, things
    are different (thanks Guido and all the folks making Python better each
    day!)

    --
    Gabriel Genellina
     
    Gabriel Genellina, Feb 5, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Achim Domma (SyynX Solutions GmbH)
    Replies:
    0
    Views:
    2,290
    Achim Domma (SyynX Solutions GmbH)
    Mar 6, 2006
  2. Fredrik Lundh

    [newbie] Confused with raise w/o args

    Fredrik Lundh, Feb 14, 2005, in forum: Python
    Replies:
    3
    Views:
    350
  3. ernest
    Replies:
    2
    Views:
    308
    Roy Smith
    Nov 14, 2010
  4. Jack Bates
    Replies:
    0
    Views:
    295
    Jack Bates
    May 2, 2011
  5. bvdp

    Raise X or Raise X()?

    bvdp, Mar 11, 2012, in forum: Python
    Replies:
    10
    Views:
    406
    Stefan Behnel
    Mar 12, 2012
Loading...

Share This Page