13 odpowiedzi
+ 6
# Hi, Nusaiba 🇵🇸!
# You can try this simple code.
# If you input a number, it calculates 100 divided by that number.
# Otherwise, it throws an exception.
# The error is caught, so the code will continue running if the exception is a ValueError.
# This will happen if you input a non-numeric value.
# If another error occurs, such as division by zero, the 'finally' block will still execute;
# otherwise, the rest of the code will stop running.
n = input()
try:
print(f"n = {repr(n)}")
n = int(n)
except ValueError:
print(f"Input value must be a number!")
else:
print(f"\nn = {n} -> 100/n = {100/n:.4f}")
finally:
print(f"\n\t<< The finally line is executed... >>")
print("\n\t...program end...")
# https://sololearn.com/compiler-playground/c1Jksmcgxz5I/?ref=app
+ 2
With the try, except block you tell python to try to execute an execution, because at first you know that it can cause problems. To know when to use it you have to know if that part of the code can throw an error and then you apply it to control the error. You must keep in mind that when python finds a code error it does not know what to do and throws a general error, but if you apply the try in the except you can handle it, for example a user entered a number where it was a string, you tell python that If the error is a data type it returns "the format is not appropriate" :)
+ 1
So I know exactly what type of error it is going to throw that is why i am using these? Right?
+ 1
Please am new here can I get more explanation to this
+ 1
Même moi j'ai pas compris
+ 1
The block of code you want to try running.The block of code to run if an exception occurs in the try block.The block of code that will run no matter what, whether an exception occurred or not.When you have code that might cause an error.To handle the error and execute alternative code.To clean up resources, like closing files, regardless of whether an error occurred.Code in try block runs first.If an exception occurs, the rest of the try block is skipped, and the except block runs.If no exception occurs, the except block is skipped.The finally block always runs after the try and except blocks, whether an exception occurred or not.
+ 1
(try, except, finally) are used for exception handling,
1.HOW DO WE USE IT AND BEACUSE?
- They are used in order to handle common errors in Python, so that our program can continue with execution, thus avoiding the error.
2.How do you run the code after using them?
- It is executed depending on the error you want or want to avoid.
Using exceptions helps us avoid errors and be able to continue with the operation of the program depending on which alternatives it should follow, I recommend that you use them
+ 1
Arya Rajeshwar Jannawar thnks for the explanation.
+ 1
Don't worry, if you have any questions about the Python language, I will be happy to help you.
0
Thank you so much😊
You explained it in a very simple way
0
Appreciate it.
0
Print=("Leval UP")
0
STE4LPH thank you so much, I appreciate it.