NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Exception handling allows us to handle and manage errors that occur during the execution of the program. It helps us to catch and respond to exceptional situations gracefully, rather than having program crash or terminate unexpectedly. Python provides several methods to handle exceptions.

1.try-except:
The try-except block is used to catch and handle exceptions. if we put the code that might raise an exception inside the try block, and if an exception occurs, it is caught by the corresponding except block. Here's an example:
Example:
try:
num1 = int(input("Enter a number: "))
num2 = int(input("Enter another number: "))
result = num1 / num2
print("Result:", result)
except ValueError:
print("Invalid input. Please enter a valid integer.")
except ZeroDivisionError:
print("Cannot divide by zero.")
In this example, if the user enters non-integer values or tries to divide by zero, the corresponding exception will be caught and an appropriate error message will be displayed.

2.try-except-else:
The try-except-else block is used when we want to execute a piece of code only if no exceptions occur. The code in the else block will run if no exceptions are raised in the try block. Example:
try:
num = int(input("Enter a number: "))
result = 100 / num
except ValueError:
print("Invalid input. Please enter a valid integer.")
except ZeroDivisionError:
print("Cannot divide by zero.")
else:
print("Result:", result)

In this example, if the user enters a valid number, the division operation will be performed and the result will be displayed. If an exception occurs, the corresponding error message will be printed.

3.try-except-finally:
The try-except-finally block is used when we want to execute a piece of code regardless of whether an exception occurs or not. The code in the finally block will always run, even if an exception is raised.
Example:
try:
file = open("example.txt", "r")
content = file.read()
print(content)
except FileNotFoundError:
print("File not found.")
finally:
file.close()
print("File closed.")
In this example, the code tries to open and read a file. If the file is not found, an exception is caught and an appropriate error message is printed. Regardless of the exception, the file is closed in the finally block.

4.raise:
The raise statement is used to explicitly raise an exception. we can use it to signal an error condition in your code.
Example:
def validate_age(age):
if age < 0:
raise ValueError("Age cannot be negative.")
elif age > 120:
raise ValueError("Invalid age. Please enter a valid age.")
else:
print("Age is valid.")

try:
age = int(input("Enter your age: "))
validate_age(age)
except ValueError as e:
print(str(e))
In this example, the validate_age() function raises a ValueError if the age is negative or greater than 120. The raised exception is caught in the try-except block, and the error message is printed.
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.