Select the true statement about the try except block in relation to the following example
The 2 statement is comprised of a 3 block and either a 4 block, a 5 block, or both. The code in the 3 block is executed first, and if it throws an exception, the code in the 4 block will be executed. The code in the 5 block will always be executed before control flow exits the entire construct. Show Try itSyntax
9The statements to be executed. 0Statement that is executed if an exception is thrown in the 3-block. 2 OptionalAn optional identifier to hold the caught exception for the associated 4 block. If the 4 block does not utilize the exception's value, you can omit the 2 and its surrounding parentheses, as 6. 7Statements that are executed before control flow exits the 8 construct. These statements execute regardless of whether an exception was thrown or caught.DescriptionThe 3 statement always starts with a 3 block. Then, a 4 block or a 5 block must be present. It's also possible to have both 4 and 5 blocks. This gives us three forms for the 3 statement:
Unlike other constructs such as 9 or 0, the 3, 4, and 5 blocks must be blocks, instead of single statements.
A 4-block contains statements that specify what to do if an exception is thrown in the 3-block. If any statement within the 3-block (or in a function called from within the 3-block) throws an exception, control is immediately shifted to the 4-block. If no exception is thrown in the 3-block, the 4-block is skipped.The 5 block will always execute before control flow exits the 8 construct. It always executes, regardless of whether an exception was thrown or caught.You can nest one or more 3 statements. If an inner 3 statement does not have a 4-block, the enclosing 3 statement's 4-block is used instead.You can also use the 3 statement to handle JavaScript exceptions. See the JavaScript Guide for more information on JavaScript exceptions.Unconditional catch-blockWhen a 4-block is used, the 4-block is executed when any exception is thrown from within the 3-block. For example, when the exception occurs in the following code, control transfers to the 4-block.
The 4-block specifies an identifier ( 4 in the example above) that holds the value of the exception; this value is only available in the scope of the 4-block.Conditional catch-blocksYou can create "Conditional 4-blocks" by combining 2 blocks with 8 structures, like this:
A common use case for this is to only catch (and silence) a small subset of expected errors, and then re-throw the error in other cases:
The exception identifierWhen an exception is thrown in the 3-block, 0 (i.e., the 4 in 2) holds the exception value. You can use this identifier to get information about the exception that was thrown. This identifier is only available in the 4-block's scope. If you don't need the exception value, it could be omitted.
The finally-blockThe 5 block contains statements to execute after the 3 block and 4 block(s) execute, but before the statements following the 8 block. Control flow will always enter the 5 block, which can proceed in one of the following ways:
If an exception is thrown from the 3 block, even when there's no 4 block to handle the exception, the 5 block still executes, in which case the exception is still thrown immediately after the 5 block finishes executing.The following example shows one use case for the 5-block. The code opens a file and then executes statements that use the file; the 5-block makes sure the file always closes after it is used even if an exception was thrown.
Control flow statements ( 1, 2, 3, 4) in the 5 block will "mask" any completion value of the 3 block or 4 block. In this example, the 3 block tries to return 1, but before returning, the control flow is yielded to the 5 block first, so the 5 block's return value is returned instead.
It is generally a bad idea to have control flow statements in the 5 block. Only use it for cleanup code.ExamplesNested try-blocksFirst, let's see what happens with this:
Now, if we already caught the exception in the inner 3-block by adding a 4-block:
And now, let's rethrow the error. 0Any given exception will be caught only once by the nearest enclosing 4-block unless it is rethrown. Of course, any new exceptions raised in the "inner" block (because the code in 4-block may do something that throws), will be caught by the "outer" block.Returning from a finally-blockIf the 5-block returns a value, this value becomes the return value of the entire 09 statement, regardless of any 1 statements in the 3 and 4-blocks. This includes exceptions thrown inside of the 4-block: 1The outer "oops" is not thrown because of the return in the 5-block. The same would apply to any value returned from the 4-block.
How does tryThe try block lets you test a block of code for errors. The except block lets you handle the error. The else block lets you execute code when there is no error. The finally block lets you execute code, regardless of the result of the try- and except blocks.
How does tryThe try statement works as follows. First, the try clause (the statement(s) between the try and except keywords) is executed. If no exception occurs, the except clause is skipped and execution of the try statement is finished. If an exception occurs during execution of the try clause, the rest of the clause is skipped.
Why is using a tryWhat is the reason for the try-except-else to exist? A try block allows you to handle an expected error. The except block should only catch exceptions you are prepared to handle. If you handle an unexpected error, your code may do the wrong thing and hide bugs.
Can one block of except statements handle multiple exceptions in Python?In Python, try-except blocks can be used to catch and respond to one or multiple exceptions. In cases where a process raises more than one possible exception, they can all be handled using a single except clause.
|