Exception handling - Python3 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Exception handling - Python3

Even though exception handling is pretty self-explaining I still wonder if one would really need this kind of functionality, because as I perceived it so far exception handling could as well be removed by just adding a few more if, elif and else conditions to the code. Please instruct me if I am wrong, or to say it more frankly, I am pretty sure I have overlooked something. If one would be so kind as to explain me exceptions more detailed and/or give an example, I would be very grateful for that.

14th Jan 2017, 2:56 PM
H3LL0FR14ND
H3LL0FR14ND - avatar
3 Answers
+ 5
the use of if elif and else will slow your programs . think about it, if your program is working perfectly and the user have not done any thing wrong, then your program will not go in any exception and the the use of if elif and else will only slow your program. when an error or exception happen in your program, the operating system (like unix, windows etc) will send a specific signal to notify your rogram about the exception, so when you write exception handling you are teaching your program to understand that specific signal. so, as a conclusion: *) we dont have to use conditions to go out of exceptions and if we are sure that we will go in an error then we have to change our code because it is wrong. *)we know that the operating system will notify our program about the exception . so we have only to teach him that at this signal do{ some code}. *)note that if we have not done the exception handling and the error occured then the program will stop working ( the signal recieved from operating system will kill the program because it does not know what to do).
14th Jan 2017, 3:19 PM
Ihab Taleb
Ihab Taleb - avatar
+ 2
Let's create a theoretical scene where You (The Programmer) have made a program that will Open Elevator Doors. (Yes, I know, strange :P) It runs through the basics: - Open door - Stay open if resistance is felt - Close doors - Switch to Up/Down Mode etc. But if some people got stuck in there, you'd have to be called in to Free Them! How, well... you coded it, so you know! But, the interface just greets you with the many, many, Many if-elif-else statements. Now, the above situation is a complete nightmarish-realm to explore. So to avoid hassle, you would include exception-handling to stop things before "it's too late". AppendError ? Good, you know there's something that needs fixing, etc. I hope I helped. :)
14th Jan 2017, 3:01 PM
ghostwalker13
ghostwalker13 - avatar
+ 2
Exception handling is the most useful part of any programming language, but you can never see it's use until you actually start a real project that can be on the Web, like a website or an app, It handles any type of error, You must have come across errors 400, 404, 500, 403, those error can not be handled with if else statements, but you can catch them with exception handling, You can ask yourself what happens when my server is down and a user/visitor is trying to access my website, the only thing that will tell the user heay we are sorry an error occurred is the exception handled, Just don't worry with time everything will start making sense to you.
4th May 2020, 11:59 PM
ugochukwu Joseph
ugochukwu Joseph - avatar