Interpretation of Dive into Python on exceptions

Asked

Viewed 46 times

5

Here is an excerpt from page 47 of the book 'Dive Into Python' about exceptions:

"...You don’t need to Handle an Exception in the Function that raises it. If one Function doesn’t Handle it, the Exception is passed to the Calling Function, then that Function’s Calling Function, and so on "up the stack." If the Exception is Never handled, your program will crash, Python will print a "traceback" to standard error, and that’s the end of that. Again, Maybe that’s what you want; it depends on what your program does."

I don’t understand exactly what he means by the So on, up the Stack and the process leading up to this statement. As well as "Exceptions is passed to the called Function..."?

Something else:

In PEP 20 there is the following note: "Explicit is better than implicit". Does this serve to raise the exceptions in a specific way? Using only generic except does not guarantee a greater coverage of the problem? So why not use it?

2 answers

6


You need to read: What are and where are the "stack" and "heap"?. So the functions are called and organized like a stack, and when an exception is thrown, until it is captured by some function, it shuts down the functions that are in the call stack, until if no function does anything with it ends up breaking the application (which is good in many cases because it is programming error). What is happening is what we call the bubbling of the exception.

Calling for:

Pilha de chamada de algumas funções

If the treatment is only in main():

Exceção borbulhando até a main

And if it is treated right at the first function on top of the stack:

Lança a exceção e já pega na função chamadora

The exception is a form of flow control that sends for the first except within the dynamic scope (different from lexical scope that everyone knows), that is, within the flow of execution that actually occurred. Some function that is running must have this command that captures the exception, it can be up to itself.

The most common is that the function that called the other that launched the exception is who will capture, at least when the exception is used correctly. In some cases it happens correctly to move to two or three call levels.

Exception is a flow control that you do not know where you will stop just by looking at the code, only during the execution it happens as the function calls occur.

To be explicit has nothing to do with the subject, to be explicit is not to be specific, they are very different things.

It is also useful to see how to deal with exceptions and because it must deal with specific exceptions. So being the most specific when it makes sense in each case is the most correct.

4

To answer the first question, consider that you have a Python program with N functions F1 until FN. In which F1 throws an exception. Suppose also in your program, F1 is called within F2, F2 inside F3 and so forth... and that FN is called in the main thread. What the author is doing is referencing to this concept, explaining that all these calls form a pile of execution in which the exception launched in a given function Fi and untreated by it is automatically passed to function Fi+1 previously inserted into the stack, until you reach your main program (up the stack), making him "crash".

Regarding the second question, I think Maniero has already given the best answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.