i dont understand python exeptions | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 7

i dont understand python exeptions

i understand exceptions and i dont really understand the use except debugging but, i dont understand what raising does and what its for thanks for the help :)

23rd Jun 2018, 10:48 PM
mYstic - avatar
8 ответов
+ 14
Exceptions are errors. Since there are different types of exceptions, when one happens in your code you’ll know what’s wrong and how to fix it. For example: inp = input() x = int(inp) Here, you’re expecting an int as input. But the user may enter a non-numeric value, like “hello”. Then the int() function will raise a ValueError exception. This will let you know what went wrong. Another thing is the raise statement. Here you’ll raise an exception yourself. This can be useful if you’re making a long code for other people to use, and want them to know if they’re doing something wrong. For example: m = input(“Enter mass: “) mass = float(m) if mass < 0: raise ValueError(“A mass must be a positive number”)
24th Jun 2018, 12:11 AM
Pedro Demingos
Pedro Demingos - avatar
+ 3
in the lesson: raising exceptions i also dont understand this: Exceptions can be raised with arguments that give detail about them. For example: name = "123" raise NameError("Invalid name!") Try It Yourself Result: >>> NameError: Invalid name!
23rd Jun 2018, 10:51 PM
mYstic - avatar
+ 1
I had a problem with exceptions and especially "raise statement" too. what I understand ... In general, you coding in a usual structure: *************************** 1- Try: 2- .... 3- except ValueError: 4- .... 5- except Exception: 6- .... 7- else: 8- .... 9- finally: 10- .... ************************* - The code first run "Try statement". - If there were any Errors, it refers to "except" lines. (line 3 or 5 in this example) - If there wasn't any Error, it refers to line 7. - Without considering there was Error or not in Try statement, "finally" statement run at the end. But When you want manually run an exception, you can use raise. for example in below code, after line 3 the code goes line 6 for run "Exception" that has said in line 3. *********************** 1- Try: 2- .... 3- raise Exception 4- except ValueError: 5- .... 6- except Exception: 7- .... 8- else: 9- .... 10- finally: 11- .... ************************* For more details: I saw below video and I think this video can help about exceptions, raise and finally: https://www.youtube.com/watch?v=NIWwJbo-9_8
25th Jul 2018, 12:45 PM
Reza Kheyrandish
Reza Kheyrandish - avatar
+ 1
exceptions are Not An Error, they are way to process situation in special way, out of usual sequence of statement flow. Thouse situations are predicted, awaited and kindly handled, so exceptions are AntiErrors. Exceptions just a way to avoid tons of Ifs in code, and add some call stack logic, already known from old times as handler registration
23rd Aug 2018, 7:21 PM
Арсений Чеботарёв
Арсений Чеботарёв - avatar
+ 1
Reza Kheyrandish nice video tutorial....really helped..
18th Mar 2019, 12:47 PM
gafar femi
gafar femi - avatar
ohh so raising is to make sure what the input is will be logical or what you want thanks
24th Jun 2018, 2:31 PM
mYstic - avatar
what i understand it means ....we are predetecting the error before being the file run
26th Jun 2018, 3:54 AM
Devanshi Kharat
Devanshi Kharat - avatar
so thanks for all your replies........... but please help me with the output , in the sense what should we type in the space given after the question asking for the output
16th Aug 2021, 10:43 AM
Sahana ca
Sahana ca - avatar