There are two approaches that can be followed when we want to deal with exceptions that occur due to unusual events:
- LBYL - Look Before You Leap
- EAFP - Easier to Ask for Forgiveness than Permission
In the LBYL approach, we avoid exceptions, while in the EAFP approach, we handle exceptions.
First, let us see the “Look before You Leap” approach. In this approach, we use conditional checks to eliminate any possibility of error. Whenever we have to perform any error-prone operation, first, we make sure that all conditions are favorable for the execution of that operation. We check all the situations that can make the operation give errors and we do this by placing conditional checks in the form of if statements. When the conditions are favourable, then only we execute the operation. If there is any possibility of error, we do not execute the operation.
if girls == 0: print('No girls, Ratio not defined') else: print('Ratio of boys to girls is', boys / girls)
So, we prevented the error from occurring by putting an if statement. In the LBYL approach, there will be a lot of checking done before the operation actually executes because we have to make sure that nothing goes wrong while performing that operation. That is why there will be lot of if statements when we write our error handling code using this coding style.
The other approach is EAFP, “Easier to ask for forgiveness than permission.” This is based on a famous quote by Grace Hooper. In this approach, we try to execute the code assuming that everything will work correctly, but if our assumption proves false and something goes wrong, we deal with it. Python supports this approach with the help of try…except statement. We will see the details of this statement in the coming sections.
try: print('Ratio of boys to girls is ', boys / girls) except ZeroDivisionError: print('No girls, Ratio not defined')
In the LBYL approach, we are very cautious; we do not give Python a chance to raise the exception, while in EAFP, we just go ahead and execute the operation, and if anything unusual occurs and Python raises an exception, then we just handle that exception. From these two approaches, EAFP is more Pythonic; it is commonly used by Python programmers, while LBYL is common in other languages like C, which don’t have any exception-handling mechanism.
Let us compare the two approaches and see some benefits of using the EAFP approach.
Since exceptions are rare, the code will work correctly most of the time. In LBYL, all the if statements are processed every time the code is run, even when everything is okay. This increases the execution time. The extra conditions put extra overhead on code processing. In EAFP, the error handling mechanism is processed only when an exception occurs, not every time the code runs. So, this approach results in efficient running in usual cases when the error does not occur. We know that most of the time, errors do not occur, so using this approach results in better performance of our program. In LBYL, sometimes you must duplicate a part of the operation in the conditional check, which again results in extra processing.
Another advantage of the EAFP approach is that the error handling code does not get mixed with your mainstream code. The error handling code is separate from the mainstream code; the mainstream code is in one block, and the error handling code is in a separate block. This separation makes the program more readable and easier to debug or modify. In LBYL, the if statements are mixed with the main logic of the program. This reduces program clarity and readability and makes it difficult to understand or modify the program thus, code management becomes difficult. In EAFP, the emphasis is on the mainstream code, while in LBYL, the emphasis is on the conditions, and the main code gets rather hidden at the end, which is not good for program readability.
Sometimes it is not possible to predict exactly what errors can occur in an operation, or the programmer may miss checking some conditions. In those cases, it is better to attempt the operation and then catch the error.
Another problem with LBYL approach is that sometimes the circumstances can change between the looking and the leaping step, so when you checked the conditions, everything was fine but while attempting the operation, something goes wrong; this could happen in a multithreaded environment. If we use EAFP approach, there will be no such problem.
标签:code,handle,Python,LBYL,EAFP,error,operation,approach From: https://www.cnblogs.com/zhangzhihui/p/18335454