+ 1
What is the purpose of raising exception ?
I don't know the meaning and purpose of that. What is the different between try/except and raising exception ?
3 Réponses
+ 9
consider this example,
say you wrote a simple server script using python which divides two numbers. say user will send two numbers to server, server will compute the result and give it back to client.
now say user sends 10 and 0. server will try to divide 10 by 0. hmm but you cannot divide number by zero so it will cause a runtime exception . This exception is unhandled so simply your server will crash and noone can connect to your server.
Now what if server knew what to do when such exception happens? yes the user who requested to divide by 0 will not get his answer, but important thing is your server will not crash
+ 6
Interpreter may tell you about syntactical errors, but it cannot detect such runtime errors , because they depend upon other factors such as user input
+ 1
try/except will handle errors that have been raised. When you raise an exception it means you are creating one in order for it to be handled. You can make custom exceptions which might be useful for an application you're making, and that exception can be handled accordingly. (in the example given by code learner, Python itself raises this error, so exceptions can be from Python or the programmer)