raise or not to raise [Newbie]

J

Jacol

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
 
P

Paddy

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:
.... print "Print this then raise an error"
.... raise Exception("Bye Bye")
....Print this then raise an error
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>

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.
 
R

Robert Kern

Jacol said:
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
 
B

Bruno Desthuilliers

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...
 
G

Gabriel Genellina

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!)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top