Differences between revisions 21 and 22
Revision 21 as of 2004-11-26 18:43:56
Size: 6776
Editor: 81-208-106-71
Comment: Traduzione in italiano del documento originale in inglese
Revision 22 as of 2004-11-26 19:40:36
Size: 3849
Editor: 212-32-244-232
Comment: extract Italian translation to separate page
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
= Manutenere le Eccezioni =
Line 6: Line 4:

Un semplice modo per manutenere le eccezioni è usare il blocco "try-except" --> "prova all'innfuoridi"
Line 19: Line 15:

Se volete esaminare l'eccezione dal codice,guardate qua:
Line 34: Line 28:
== Prendiamo il controllo generale del codice ==
Line 37: Line 29:

Talvolta,potete prendere "all" tutti gli errori che è possibile generare, ma di solito non è possibile.Nella mag-
gior parte dei casi,quanto specificate tanto è possibile (Wiki:CatchWhatYouCanHandle). Nel primo caso, se stavate usando una clausola eccezione incontrata e l'utente ha premuto Ctrl-C,generando una KeyboardInterrupt (interruzione
da tastiera),non desiderate che il programma emetta il messaggio "divide by zero"--> (divisione per zero.)
Line 44: Line 32:
Tuttavia,ci sono alcune situazioni in cui è meglio prendere ''all'' tutti gli errori
Line 47: Line 33:
Per esempio, supponiamo che scriviate un modulo per un servizio web. Vorrete che le informazioni inerenti gli errori sia riportate in una pagina web, e il server continui a girare,se tutto ciò è possibile.Ma voi non avete idea che tipo di errori dovete mettere nbel vs codice.
  
 
Line 51: Line 35:
In una situazio9ne di questo tipo,il codice potrebbe essere simile a questo:
Line 65: Line 48:
MoinMoin software è un buon esempio su dove in generale catturare gli errori. Se scrivte Moin``Moin macro estensione,e diamo avvio ad un errore,Moin''Moin prepareà un dettagliato rapporto circa l'errore e la catena di eventi che conduce a lui. Il software Python deve essere abilitato a catturare tutti gli errori, e li spedisce poi alla pagina web.
Line 69: Line 50:
== Ricerca di Nomi Specifici di Eccezioni ==
Line 72: Line 51:

Eccezioni Standard che possono essere emesse sono dettaglaite in questo sito:
Line 78: Line 55:
Osservate la Classe documentazione per trovare che eccezioni una data classe può emeyttere.
Line 82: Line 58:
= Vedi Anche =
Line 85: Line 59:

Su questa wiki:WritingExceptionClasses, TracebackModule.
Line 90: Line 62:
Per idde generali(non specificate in Python) circa le eccezioni,consulatre Wiki:ExceptionPatterns.
Line 94: Line 64:
= Per scrivere sull'argomento......."

* Give example of IOError, and interpreting the IOError code.

  * Dammi un esempio di IOError, e interpretiamo il codice di errore
  * Give example of IOError, and interpreting the IOError code.
Line 102: Line 68:
  * Dammi un esempio di eccezioni multiple. Manteniamo le eccezioni multiple in linea
Line 105: Line 69:

  * Mostra come usare "else" e "finalità"
Line 110: Line 72:
  * Mostra come continuare con un "rialzo"
Line 113: Line 73:

= Domande =
Line 118: Line 76:
== Manutenzione generale degli errori ==
Line 121: Line 77:

Nella sezione "manutenzione generale degli errori", si è detto che per la cattura degli errori userete questo codice:
Line 136: Line 90:
Tuttavia in origine era:
Line 148: Line 100:
Alcune indicano che "except" catturano piu di un semplice "except Exception, e."
Line 151: Line 101:

''Perchè è questo il caso??.Qual è la differenza?'' --LionKimbro.
Line 156: Line 104:
Per ora ( nella ver. 2.3) l'eccezionempm ha ereditato da Exception. Allora pianifichiamo 'except:' catturiamo tutte le eccezioni,non solo quelle di sistema. -- MikeRovner [[DateTime(2004-01-19T05:49:19Z)]]

Line 161: Line 106:
== Prendiamo utili Informazioni da una eccezione ==
Line 164: Line 107:

Cosi si pò avere qualcosa di simile:
Line 174: Line 115:
... e Python soffia indietro
Line 181: Line 121:
.... e cosi,naturalmente chiederete, "bene che, cosa 'era' in {{{d}}}?" You know- you can put a {{{print d}}} in there, and that works. But is there a better, more interesting way to get at that information that people know of?
Line 183: Line 123:
You know- you can put a {{{print d}}} in there, and that works. But is there a better, more interesting way to get at that information that people know of? ----
See also: Italian translation at ManutenereLeEccezioni.

Handling Exceptions

The simplest way to handle exceptions is with a "try-except" block:

   1 (x,y) = (5,0)
   2 try:
   3   z = x/y
   4 except ZeroDivisionError:
   5   print "divide by zero"

If you wanted to examine the exception from code, you could have:

   1 (x,y) = (5,0)
   2 try:
   3   z = x/y
   4 except ZeroDivisionError, e:
   5   z = e # representation: "<exceptions.ZeroDivisionError instance at 0x817426c>"
   6 print z # output: "integer division or modulo by zero"

General Error Catching

Sometimes, you want to catch all errors that could possibly be generated, but usually you don't.In most cases, you want to be as specific as possible (CatchWhatYouCanHandle). In the first example above, if you were using a catch-all exception clause and a user presses Ctrl-C, generating a KeyboardInterrupt, you don't want the program to print "divide by zero".

However, there are some situations where it's best to catch all errors.

For example, suppose you are writing an extension module to a web service. You want the error information to output the output web page, and the server to continue to run, if at all possible. But you have no idea what kind of errors you might have put in your code.

In situations like these, you may want to code something like this:

   1 import sys
   2 try:
   3   untrusted.execute()
   4 except: # catch *all* exceptions
   5   e = sys.exc_info()[1]
   6   write_to_page( "<p>Error: %s</p>" % e )

MoinMoin software is a good example of where general error catching is good. If you write MoinMoin extension macros, and trigger an error, MoinMoin will give you a detailed report of your error and the chain of events leading up to it. Python software needs to be able to catch all errors, and deliver them to the recipient of the web page.

Finding Specific Exception Names

Standard exceptions that can be raised are detailed at:

Look to class documentation to find out what exceptions a given class can raise.

See Also:

On this wiki: WritingExceptionClasses, TracebackModule.

For general (non-Python specific) ideas about exceptions, consult ExceptionPatterns.

To Write About...

  • Give example of IOError, and interpreting the IOError code.
  • Give example of multiple excepts. Handling multiple excepts in one line.
  • Show how to use "else" and "finally".
  • Show how to continue with a "raise".

Questions

General Error Handling

In the "general error handling" section above, it says to catch all exceptions, you use the following code:

   1 import sys
   2 try:
   3   untrusted.execute()
   4 except: # catch *all* exceptions
   5   e = sys.exc_info()[1]
   6   write_to_page( "<p>Error: %s</p>" % e )

However, it originally was:

   1 try:
   2   untrusted.execute()
   3 except Exception, e:
   4   write_to_page( "<p>Error: %s</p>" % str(e) )

Someone pointed out that "except" catches more than just "except Exception, e."

Why is that the case? What is the difference?-- LionKimbro

For now (version 2.3) exception doesn't have to be inherited from Exception. Thus plain 'except:' catches all exceptions, not only system. -- MikeRovner DateTime(2004-01-19T05:49:19Z)

Getting Useful Information from an Exception

So, I've got something like:

   1 (a,b,c) = d

...and Python spits back:

ValueError: unpack list of wrong size

...and so, you naturally wonder, "Well, what was in d?"

You know- you can put a print d in there, and that works. But is there a better, more interesting way to get at that information that people know of?


See also: Italian translation at ManutenereLeEccezioni.

HandlingExceptions (last edited 2024-03-05 20:29:39 by MatsWichmann)

Unable to edit the page? See the FrontPage for instructions.