Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!

This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join 11 million other learners and get started learning Python for data science today!

Good news! You can save 25% off your Datacamp annual subscription with the code LEARNPYTHON23ALE25 - Click here to redeem your discount

Gestione delle Eccezioni


Quando programmi, gli errori accadono. È solo un dato di fatto. Forse l'utente ha fornito input errati. Forse una risorsa di rete non era disponibile. Forse il programma ha esaurito la memoria. Oppure il programmatore potrebbe aver commesso un errore!

La soluzione di Python agli errori sono le eccezioni. Potresti aver già visto un'eccezione prima.

print(a)

#errore
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined

Ops! Hai dimenticato di assegnare un valore alla variabile 'a'.

Ma a volte non vuoi che le eccezioni fermino completamente il programma. Potresti voler fare qualcosa di speciale quando viene sollevata un'eccezione. Questo viene fatto in un blocco try/except.

Ecco un esempio banale: supponiamo che tu stia iterando su una lista. Devi iterare su 20 numeri, ma la lista è composta da input dell'utente e potrebbe non avere 20 numeri. Dopo aver raggiunto la fine della lista, vuoi che il resto dei numeri venga interpretato come uno 0. Ecco come potresti fare:

def do_stuff_with_number(n):
    print(n)

def catch_this():
    the_list = (1, 2, 3, 4, 5)

    for i in range(20):
        try:
            do_stuff_with_number(the_list[i])
        except IndexError: # Raised when accessing a non-existing index of a list
            do_stuff_with_number(0)

catch_this()

Ecco, non è stato troppo difficile! Puoi farlo con qualsiasi eccezione. Per maggiori dettagli sulla gestione delle eccezioni, non guardare oltre i Python Docs

Exercise

Gestisci tutte le eccezioni! Ripensa alle lezioni precedenti per restituire il cognome dell'attore.

# Setup actor = {"name": "John Cleese", "rank": "awesome"} # Function to modify!!! def get_last_name(): return actor["last_name"] # Test code get_last_name() print("All exceptions caught! Good job!") print("The actor's last name is %s" % get_last_name()) actor = {"name": "John Cleese", "rank": "awesome"} def get_last_name(): return actor["name"].split()[1] get_last_name() print("All exceptions caught! Good job!") print("The actor's last name is %s" % get_last_name()) test_output_contains("Cleese") test_output_contains("All exceptions caught! Good job!") test_output_contains("The actor's last name is Cleese") success_msg("Great work!")

This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!

Previous Tutorial Next Tutorial Take the Test
Copyright © learnpython.org. Read our Terms of Use and Privacy Policy