[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Ignore exceptions

SMALLp

1/24/2008 8:13:00 PM

Hy. Is there any way to make interrupter ignore exceptions. I'm working
on bigger project and i used to put try catch blocks after writing and
testing code what's boring and it's easy to make mistake. I remember of
something like that in C++ but I cant find anythin like that for python.

SMALLp
15 Answers

Mike Driscoll

1/24/2008 8:27:00 PM

0

On Jan 24, 2:13 pm, SMALLp <po...@mzm.hr> wrote:
> Hy. Is there any way to make interrupter ignore exceptions. I'm working
> on bigger project and i used to put try catch blocks after writing and
> testing code what's boring and it's easy to make mistake. I remember of
> something like that in C++ but I cant find anythin like that for python.
>
> SMALLp

See the try statement:

http://docs.python.org/re...
http://www.network-theory.co.uk/docs/pytut/HandlingExcep...
http://docs.python.org/api/exceptionHan...

Mike

Grant Edwards

1/24/2008 8:29:00 PM

0

On 2008-01-24, SMALLp <pofuk@mzm.hr> wrote:

> Hy.

Hi.

> Is there any way to make interrupter ignore exceptions.

Nope. Either handle the exceptions or write code that doesn't
generate exceptions.

> I'm working on bigger project and i used to put try catch
> blocks after writing and testing code

You're getting unhandled exceptions after you've tested your
code? I guess you need to do better testing.

> what's boring and it's easy to make mistake. I remember of
> something like that in C++ but I cant find anythin like that
> for python.

I should hope not. The Python language wouldn't _work_ if the
VM ignored exceptions. Exceptions are used for all sorts of
things besides errors (terminating a loop, exiting a program,
etc.). If exceptions were ignored all sorts of things would
stop working.

--
Grant Edwards grante Yow! I'll show you MY
at telex number if you show me
visi.com YOURS ...

Jonathan Gardner

1/24/2008 9:31:00 PM

0

On Jan 24, 12:13 pm, SMALLp <po...@mzm.hr> wrote:
> Hy. Is there any way to make interrupter ignore exceptions. I'm working
> on bigger project and i used to put try catch blocks after writing and
> testing code what's boring and it's easy to make mistake. I remember of
> something like that in C++ but I cant find anythin like that for python.
>

Hello. Two points with exceptions.

* Only write try blocks when you are actually going to do something
interesting with the exception. Otherwise, let the exception bubble
up.

* If you have unwanted exceptions, fix the root cause of the exception
rather than try to hide the exception. The exception is saying
something is wrong and needs attention. Provide that attention.

I have seen too many people write code in Java that simply masks all
exceptions because they don't want to write the interface to their
functions that describes what exceptions are possible. I have seen
comments around this code saying, "This should always work." Of
course, it doesn't always work and when it doesn't work, they don't
know about it and they don't even know how to tell what went wrong.

The emotion driving this exception masking practice I see in the Java
world is laziness, not correctness. This is not the good form of
laziness (where you want to write good code so you end up doing less
work) but the bad form (where you don't want to work at all).

There is no need to mask exceptions in Python. In fact, it is more
work to mask exceptions, and you should feel bad about all the extra
typing you are doing.

Once again: All try blocks should do something interesting when they
catch an exception. No exception should be ignored or thrown away.

A few sample good uses of try/except blocks:

(1) Do something else if an expected exception occurs.

try:
# There is a good chance an exception will be thrown. If so, I
want to do something else.
d['foo'] += 5
except KeyError:
d['foo'] = 5

(2) Show a friendly error message when an exception occurs over a
significant chunk of the program. (Useful for websites and GUI apps.)

try:
# There is a very complicated piece of code. Any of a million
exceptions could occur.
...
except:
# Show a nicely formatted error message with hints on how to debug
the error.
...

Here are some bad examples:

(BAD)

try:
# I don't know what is happening in here, but it always throws an
exception.
# I don't want to think about it because it makes my brain hurt.
...
except:
pass

(WORSE) The alternate form--try N times, masking the error each time--
is equally bad.

while True:
try:
# Something could go wrong. What could go wrong? Who cares?
...
break
except:
# We'll just keep trying forever....
pass

Roger Miller

1/24/2008 10:56:00 PM

0

On Jan 24, 11:30 am, Jonathan Gardner <jgard...@jonathangardner.net>
wrote:
> ....

> A few sample good uses of try/except blocks:
>
> (1) Do something else if an expected exception occurs.
> ...
> (2) Show a friendly error message when an exception occurs over a
> significant chunk of the program. (Useful for websites and GUI apps.)
> ...

I'd add (3) Clean-up handlers. These don't actually handle the
problem,
they just free resources, close files, etc. before re-raising the
exception
for someone else to worry about.

Scott David Daniels

1/25/2008 3:31:00 AM

0

Roger Miller wrote:
> On Jan 24, 11:30 am, Jonathan Gardner <jgard...@jonathangardner.net>
> wrote:
>> ....
>
>> A few sample good uses of try/except blocks:
>> (1) Do something else if an expected exception occurs.
>> (2) Show a friendly error message when an exception occurs over ...
> I'd add (3) Clean-up handlers. These don't actually handle the
> problem, they just free resources, close files, etc. before re-
> raising the exception for someone else to worry about.

Ah, but typically these are try/finally blocks. Many can be dealt
with by context mangers in 2.5 or later -- see with_statement.


--Scott David Daniels
Scott.Daniels@Acm.Org

Matt Giwer

6/29/2013 6:05:00 AM

0

On 6/27/2013 10:52 PM, Alex Milman wrote:
> On Tuesday, June 25, 2013 9:06:58 PM UTC-4, Matt Giwer wrote:
>> On 6/22/2013 12:14 PM, Bradipus wrote:

>>> http://www.theatlantic.com/national/archive/2013/06/no-lincoln-could-not-have-bought-the-slav...

>>> http://eh.net/encyclopedia/article/ransom.ci...

>> Or the slaves/serfs buy their own freedom as in Russia,
>
> As usual, you don't know what you are talking about. Serfs did not buy their _freedom_ in Russia: it was guaranteed by the Emancipation Manifesto of February 19 (Russian style), 1861. Read its text on the web http://ru.wikisource.org/wiki/%D0%9C%D0%B0%D0%BD%D0%B8%D1%84%D0%B5%D1%81%D1%82_19_%D1%84%D0%B5%D0%B2%D1%80%D0%B0%D0%BB%D1%8F_1861_%D0%B3%D0%BE%D0%B4%D0%B0_%D0%BE%D0%B1_%D0%BE%D1%82%D0%BC%D0%B5%D0%BD%D0%B5_%D0%BA%D1%80%D0%B5%D0%BF%D0%BE%D1%81%D1%82%D0%BD%D0%BE%D0%B3%D0%BE_%D0%BF%D1%80%D0%B0%D...

1. I have no use for anonymous sources.

2. My Russian extends to a few choice swear words and the ability to
avoid remaining sober.

Do you have something in English?

--
The worst mistake Palestinians ever made
was inventing Judaism.
-- The Iron Webmaster, 4430

AlexMilman

6/29/2013 10:22:00 PM

0

On Saturday, June 29, 2013 2:04:48 AM UTC-4, Matt Giwer wrote:
> On 6/27/2013 10:52 PM, Alex Milman wrote:
>
> > On Tuesday, June 25, 2013 9:06:58 PM UTC-4, Matt Giwer wrote:
>
> >> On 6/22/2013 12:14 PM, Bradipus wrote:
>
>
>
> >>> http://www.theatlantic.com/national/archive/2013/06/no-lincoln-could-not-have-bought-the-slav...
>
>
>
> >>> http://eh.net/encyclopedia/article/ransom.ci...
>
>
>
> >> Or the slaves/serfs buy their own freedom as in Russia,
>
> >
>
> > As usual, you don't know what you are talking about. Serfs did not buy their _freedom_ in Russia: it was guaranteed by the Emancipation Manifesto of February 19 (Russian style), 1861. Read its text on the web http://ru.wikisource.org/wiki/%D0%9C%D0%B0%D0%BD%D0%B8%D1%84%D0%B5%D1%81%D1%82_19_%D1%84%D0%B5%D0%B2%D1%80%D0%B0%D0%BB%D1%8F_1861_%D0%B3%D0%BE%D0%B4%D0%B0_%D0%BE%D0%B1_%D0%BE%D1%82%D0%BC%D0%B5%D0%BD%D0%B5_%D0%BA%D1%80%D0%B5%D0%BF%D0%BE%D1%81%D1%82%D0%BD%D0%BE%D0%B3%D0%BE_%D0%BF%D1%80%D0%B0%D...
>
>
>
> 1. I have no use for anonymous sources.
>
>
Source is anything but 'anonymous' because it is authored by Alexander II and if you want to look at the original (in old Russian), http://nasledie.russportal.ru/index.php?id=histrus.ma...

>
> 2. My Russian extends to a few choice swear words and the ability to
>
> avoid remaining sober.
>
>
>
> Do you have something in English?
>

Why would I need anything in English? You made a wrong claim and I pointed to the original document which confirms my statement that your claim is wrong. If you don't believe me, it is up to you to find something in English. Actually, one would expect that you did some relevant reading before writing your post.






>
> --
>
> The worst mistake Palestinians ever made
>
> was inventing Judaism.
>
> -- The Iron Webmaster, 4430

Matt Giwer

6/30/2013 11:31:00 AM

0

On 6/29/2013 6:21 PM, Alex Milman wrote:
> On Saturday, June 29, 2013 2:04:48 AM UTC-4, Matt Giwer wrote:
>> On 6/27/2013 10:52 PM, Alex Milman wrote:
>>
>>> On Tuesday, June 25, 2013 9:06:58 PM UTC-4, Matt Giwer wrote:
>>
>>>> On 6/22/2013 12:14 PM, Bradipus wrote:

>>>>> http://www.theatlantic.com/national/archive/2013/06/no-lincoln-could-not-have-bought-the-slav...

>>>>> http://eh.net/encyclopedia/article/ransom.ci...

>>>> Or the slaves/serfs buy their own freedom as in Russia,

>>> As usual, you don't know what you are talking about. Serfs did not buy their _freedom_ in Russia: it was guaranteed by the Emancipation Manifesto of February 19 (Russian style), 1861. Read its text on the web http://ru.wikisource.org/wiki/%D0%9C%D0%B0%D0%BD%D0%B8%D1%84%D0%B5%D1%81%D1%82_19_%D1%84%D0%B5%D0%B2%D1%80%D0%B0%D0%BB%D1%8F_1861_%D0%B3%D0%BE%D0%B4%D0%B0_%D0%BE%D0%B1_%D0%BE%D1%82%D0%BC%D0%B5%D0%BD%D0%B5_%D0%BA%D1%80%D0%B5%D0%BF%D0%BE%D1%81%D1%82%D0%BD%D0%BE%D0%B3%D0%BE_%D0%BF%D1%80%D0%B0%D...

>> 1. I have no use for anonymous sources.

> Source is anything but 'anonymous' because it is authored by Alexander II and if you want to look at the original (in old Russian), http://nasledie.russportal.ru/index.php?id=histrus.ma...

ALL wikipedias are anonymous. As to the author I have no idea who was
the author. Nor do I have any idea how it worked out in practice nor do
I expect anonymous sources to be credible.

There are idiots who cite Lincoln's Emancipation Proclamation as the
end of slavery in the US. I have no idea what the Czar's real authority
was at the time. Perhaps you could post an excuse for not being able to
find a credible source in English for that.

If you can find a credible source in English please feel free to post
the reason you cannot find such a credible source for this too.

>> 2. My Russian extends to a few choice swear words and the ability to
>> avoid remaining sober.

>> Do you have something in English?

> Why would I need anything in English? You made a wrong claim and I pointed to the original document which confirms my statement that your claim is wrong. If you don't believe me, it is up to you to find something in English. Actually, one would expect that you did some relevant reading before writing your post.

And you are an asshole but a cute asshole so I make allowances.

Why am I so blessed with the attention of idiots?

--
The worst mistake Palestinians ever made
was inventing Judaism.
-- The Iron Webmaster, 4430

AlexMilman

6/30/2013 7:14:00 PM

0

On Sunday, June 30, 2013 7:30:45 AM UTC-4, Matt Giwer wrote:
> On 6/29/2013 6:21 PM, Alex Milman wrote:
>
> > On Saturday, June 29, 2013 2:04:48 AM UTC-4, Matt Giwer wrote:
>
> >> On 6/27/2013 10:52 PM, Alex Milman wrote:
>
> >>
>
> >>> On Tuesday, June 25, 2013 9:06:58 PM UTC-4, Matt Giwer wrote:
>
> >>
>
> >>>> On 6/22/2013 12:14 PM, Bradipus wrote:
>
>
>
> >>>>> http://www.theatlantic.com/national/archive/2013/06/no-lincoln-could-not-have-bought-the-slav...
>
>
>
> >>>>> http://eh.net/encyclopedia/article/ransom.ci...
>
>
>
> >>>> Or the slaves/serfs buy their own freedom as in Russia,
>
>
>
> >>> As usual, you don't know what you are talking about. Serfs did not buy their _freedom_ in Russia: it was guaranteed by the Emancipation Manifesto of February 19 (Russian style), 1861. Read its text on the web http://ru.wikisource.org/wiki/%D0%9C%D0%B0%D0%BD%D0%B8%D1%84%D0%B5%D1%81%D1%82_19_%D1%84%D0%B5%D0%B2%D1%80%D0%B0%D0%BB%D1%8F_1861_%D0%B3%D0%BE%D0%B4%D0%B0_%D0%BE%D0%B1_%D0%BE%D1%82%D0%BC%D0%B5%D0%BD%D0%B5_%D0%BA%D1%80%D0%B5%D0%BF%D0%BE%D1%81%D1%82%D0%BD%D0%BE%D0%B3%D0%BE_%D0%BF%D1%80%D0%B0%D...
>
>
>
> >> 1. I have no use for anonymous sources.
>
>
>
> > Source is anything but 'anonymous' because it is authored by Alexander II and if you want to look at the original (in old Russian), http://nasledie.russportal.ru/index.php?id=histrus.ma...
>
>
>
> ALL wikipedias are anonymous.

Irrelevant because none of the links above are from wikipedia.

> As to the author I have no idea who was
>
> the author.

Manifest was signed by Alexander II so this is not an issue.


>Nor do I have any idea how it worked out in practice


Why not to be honest (just for change) and confess than when you wrote your post you did not have a clue on the subject. Period.



AlexMilman

6/30/2013 11:22:00 PM

0

On Sunday, June 30, 2013 7:30:45 AM UTC-4, Matt Giwer wrote:
> On 6/29/2013 6:21 PM, Alex Milman wrote:

> I have no idea what the Czar's real authority
>
> was at the time. Perhaps you could post an excuse for not being able to
>
> find a credible source in English for that.
>

You are confused (not a big surprise, taking into an account your well-known idiocy): I'm not trying to prove anything to you. Why would I bother with a moron and asshole like you? I'm just pointing to the fact that you are blabbing of the things you have no clue about. Your own statement above confirms this obvious fact.