Python Exception - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript Python Exception - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Saturday, December 1, 2018

Python Exception

Python Exceptions


Exceptions

Exceptions help you respond appropriately to errors that are likely to occur. You place code that might cause an  error in the try block. Code that should run in response to an error goes in the except block. Code that should run only if the try block was successful goes in the else block.


exception.py ( code )



#Catching an exception
prompt = "How many tickets do you need? "
num_tickets = input(prompt)

try:
num_tickets = int(num_tickets)
except ValueError:
print("please try again.")
else:
print("your tickets are printing.")


Example Screenshot 

Python Exception

No comments:

Post a Comment

Post Top Ad