0
What is Exception?
3 Réponses
+ 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.
+ 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.
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...