Differences between revisions 19 and 21 (spanning 2 versions)
Revision 19 as of 2004-03-28 01:24:23
Size: 3750
Editor: dsl254-010-130
Comment: Getting useful information from an exception dump.
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
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
= Manutenere le Eccezioni =
Line 4: Line 6:

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

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

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 32: Line 44:
Tuttavia,ci sono alcune situazioni in cui è meglio prendere ''all'' tutti gli errori
Line 33: Line 47:
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 35: Line 51:
In una situazio9ne di questo tipo,il codice potrebbe essere simile a questo:
Line 48: Line 65:
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 50: Line 69:
== Ricerca di Nomi Specifici di Eccezioni ==
Line 51: Line 72:

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

Su questa wiki:WritingExceptionClasses, TracebackModule.
Line 62: Line 90:
Per idde generali(non specificate in Python) circa le eccezioni,consulatre Wiki:ExceptionPatterns.
Line 64: Line 94:
= Per scrivere sull'argomento......."
Line 65: Line 97:
      * Dammi un esempio di IOError, e interpretiamo il codice di errore
                        
Line 66: Line 101:

  * Dammi un esempio di eccezioni multiple. Manteniamo le eccezioni multiple in linea
Line 67: Line 105:

  * Mostra come usare "else" e "finalità"
Line 68: Line 109:

  * Mostra come continuare con un "rialzo"
Line 71: Line 114:
= Domande =
Line 73: Line 118:
== Manutenzione generale degli errori ==
Line 74: Line 121:

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

''Perchè è questo il caso??.Qual è la differenza?'' --LionKimbro.
Line 101: Line 156:
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 103: Line 161:
== Prendiamo utili Informazioni da una eccezione ==
Line 104: Line 164:

Cosi si pò avere qualcosa di simile:
Line 112: Line 174:
... e Python soffia indietro
Line 118: Line 181:
.... e cosi,naturalmente chiederete, "bene che, cosa 'era' in {{{d}}}?"

Handling Exceptions

Manutenere le Eccezioni

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

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

   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:

Se volete esaminare l'eccezione dal codice,guardate qua:

   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

Prendiamo il controllo generale del codice

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

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 (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.)

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

Tuttavia,ci sono alcune situazioni in cui è meglio prendere all tutti gli errori

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

In situations like these, you may want to code something like this: In una situazio9ne di questo tipo,il codice potrebbe essere simile a questo:

   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.

MoinMoin software è un buon esempio su dove in generale catturare gli errori. Se scrivte MoinMoin macro estensione,e diamo avvio ad un errore,MoinMoin 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.

Finding Specific Exception Names

Ricerca di Nomi Specifici di Eccezioni

Standard exceptions that can be raised are detailed at:

Eccezioni Standard che possono essere emesse sono dettaglaite in questo sito:

Look to class documentation to find out what exceptions a given class can raise. Osservate la Classe documentazione per trovare che eccezioni una data classe può emeyttere.

See Also:

Vedi Anche

On this wiki: WritingExceptionClasses, TracebackModule.

Su questa wiki:WritingExceptionClasses, TracebackModule.

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

Per idde generali(non specificate in Python) circa le eccezioni,consulatre ExceptionPatterns.

To Write About...

= 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 multiple excepts. Handling multiple excepts in one line.
  • Dammi un esempio di eccezioni multiple. Manteniamo le eccezioni multiple in linea
  • Show how to use "else" and "finally".
  • Mostra come usare "else" e "finalità"
  • Show how to continue with a "raise".
  • Mostra come continuare con un "rialzo"

Questions

Domande

General Error Handling

Manutenzione generale degli errori

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

Nella sezione "manutenzione generale degli errori", si è detto che per la cattura degli errori userete questo codice:

   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:

Tuttavia in origine era:

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

Alcune indicano che "except" catturano piu di un semplice "except Exception, e."

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

Perchè è questo il caso??.Qual è la differenza? --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)

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)

Getting Useful Information from an Exception

Prendiamo utili Informazioni da una eccezione

So, I've got something like:

Cosi si pò avere qualcosa di simile:

   1 (a,b,c) = d

...and Python spits back:

... e Python soffia indietro

ValueError: unpack list of wrong size

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

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

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

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