What is Exception? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is Exception?

25th Feb 2017, 4:34 PM
Tapeshwar Kumar
Tapeshwar Kumar - avatar
3 Answers
+ 8
Hmm... let's take a case of user input. In Python, it always always produces a string. So, if you tell the user to input a number that you need for further calculations, you have to convert it to an integer. Let's say you want to divide by this number. Everything is fine until the user enters numbers. You may even secure yourself from dividing by zero, with a relevant if statement. But what if the user just presses Enter or enters a letter? Any int() function would then crash the program with an uncaught exception. That is why you have to enter the whole block of code between try-catch or try-except pair.
25th Feb 2017, 7:14 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 4
It is some kind of an error raised by the interpreter while processing the code. It could be related to an incorrect data type, wrong call of a function, division by zero and many, many others. In an interpreted language, like Python, exceptions most often terminate the program, that's why it is important to "try and catch" them in dubious situations.
25th Feb 2017, 5:29 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
0
If I know it may error then I can use if ...else.. why should I use try...catch ? May I Have Any Answer Please...
25th Feb 2017, 6:43 PM
Tapeshwar Kumar
Tapeshwar Kumar - avatar