+ 2

Quick question in python.

My question is I am having a hard time understanding try except and finally in python, how do we use it? When and why do we use? How does the code excuted after using those ? If anyone could explain it in the simplest way possible, I would appreciate it

4th Jun 2024, 8:56 PM
Nusaybah 🇵🇸
Nusaybah 🇵🇸 - avatar
13 Réponses
+ 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
4th Jun 2024, 9:32 PM
Per Bratthammar
Per Bratthammar - avatar
+ 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" :)
4th Jun 2024, 9:05 PM
Chandler
Chandler - avatar
+ 1
So I know exactly what type of error it is going to throw that is why i am using these? Right?
5th Jun 2024, 2:23 AM
Nusaybah 🇵🇸
Nusaybah 🇵🇸 - avatar
+ 1
Please am new here can I get more explanation to this
5th Jun 2024, 6:47 PM
Deborah Odinakachi
Deborah Odinakachi - avatar
+ 1
Même moi j'ai pas compris
5th Jun 2024, 11:57 PM
Mouhamed Gueye
Mouhamed Gueye - avatar
+ 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.
6th Jun 2024, 5:48 PM
Arya Rajeshwar Jannawar
Arya Rajeshwar Jannawar - avatar
+ 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
6th Jun 2024, 5:49 PM
STE4LPH
STE4LPH - avatar
+ 1
Arya Rajeshwar Jannawar thnks for the explanation.
6th Jun 2024, 6:08 PM
Nusaybah 🇵🇸
Nusaybah 🇵🇸 - avatar
+ 1
Don't worry, if you have any questions about the Python language, I will be happy to help you.
7th Jun 2024, 3:40 AM
STE4LPH
STE4LPH - avatar
0
Thank you so much😊 You explained it in a very simple way
4th Jun 2024, 9:21 PM
Nusaybah 🇵🇸
Nusaybah 🇵🇸 - avatar
0
Appreciate it.
5th Jun 2024, 2:22 AM
Nusaybah 🇵🇸
Nusaybah 🇵🇸 - avatar
0
Print=("Leval UP")
6th Jun 2024, 5:00 PM
R-åchid_i-brØ 2-G8
R-åchid_i-brØ 2-G8 - avatar
0
STE4LPH thank you so much, I appreciate it.
6th Jun 2024, 6:08 PM
Nusaybah 🇵🇸
Nusaybah 🇵🇸 - avatar