January 26, 2021
5 Python Examples for handling exceptions using try, except and finally

5 Python Examples for handling exceptions using try, except and finally Python exceptions are errors caused by syntactically correct explanations.

You stop the script. Examples of exceptions are naming error, typing error, assertion error, broken connection error and others.

These miscarriages can be addressed to prevent the unpredictable realization of a scenario. Additional info AWS Cloud support.
A description of all Python exceptions can be found here.

This training manual contains the following examples:

  1. The Python attempt, except Block.
  2. Treatment of multiple Python exceptions
  3. The python is permanently locked when an exception is made.
  4. Python at last, Bloc – if there are no exceptions.
  5. Python’s nest, except Block.

Look at the following code:

a = 12
s = hi
expression (a+s)

A way out:

Confirmation (last call) :
File, line 3, in
TypeError: unsupported operand(s) for +: ‘int’ and ‘str’.

Here the syntax of the print function is correct, but the addition of an integer and a string is incorrect. As a result, the TypeError is canceled by the Python interpreter.

1. Python experiment – except Block

The keywords for the exception treatment are try, except and finally.

The test block shall be followed by an exception block. The addition of the last block is optional.

The operations in the test block are carried out line by line. Where the execution of a request gives rise to an exception. The other instructions in this block are skipped and carried out to the beginning of the block.

For example:

a = 12
s = hi
pcb (try inside)
pcb(a + s) # will call TypeError
pcb (using original data types)
except TypeError: # will only process TypeError
print (inside except)
print(str(a) + s)
print(using datatypes)

A way out:

Indoor testing
Indoor testing other than
Printed with data types based on

Here the TypeError was triggered when line 2 in the test block was executed. As a result, the execution of the other declarations was ignored and started in addition to the bulk execution. Note that the exceptions to be worked on are specified in addition to the keyword, except.

If you are new to Python, this can help you get a head start: 5 examples of object oriented programming in Python with Jumpstart

2. Treatment of multiple exceptions to Python

Multiple exceptions can be treated with a single test block. This is done by specifying exception names separated by commas in parentheses immediately after the keyword.

Try :
if (3 + 4 – 5) > 0 :
a = 3
a.append(hello) # ejects AttributeError
else :
print(hello + 4) # ejects AttributeError
except (AttributeError, TypeError) as e :
-Print (an error has occurred:, e)

A way out:

There’s been a mistake: The object int has no attachment.

To find out more about Python, if that’s what you’re looking for, click here: 9 Python, if not elite team Examples

3. Python, eventually blocked – if exceptionoccurs.

After all, it is an O.R. that is late, except for the O.R. This instruction block is executed regardless of whether an exception has been detected or not.

Finally, add a block to the previous example:

Test :
if (3 + 4 – 5) > 0 :
a = 3
a.append(hello) # eject attribute error
else :
print(hello + 4) # eject typo
except (Attribute error, TypeError) if e
Pressure (an error has occurred :,, e)
finally :
Printout (Attribute Error Excluded (Attribute Error Failed))

A way out:

There’s been a mistake: The object int does not have the appendix
try attribute, except that the block has been executed successfully.

In this case an exception was made for instructional attempts, except that the block was executed and then eventually the block.

4. Python finally blocked – when there are no exceptions

Let’s take another example:

if (3 + 4 – 5) < 0:
a = 3
print(a + 5) # just add
print(hello + 4) # connected string
except (AttributeError, TypeError) as e :
Pressure (an error has occurred:, e)
finally :
Print output (attribute error, typing error)

A way out:

Tried and tested, except for successfully completed units

Here we can see that the block has finally been executed, even though the block has never been executed.

If the operators within the block, except and finally the block, throw an exception, the rest of the script is executed.

5. Interleaved Python, except block.

The block with exclusion attempt may be surrounded by another block with exclusion attempt.

Importing the xenon system

try:with open(hello.json) as fp:try:json_dict = json.load(fp)except json.JSONDecodeError:print(Json file doesn’t exist)print(json_dict)except json.JSONDecodeError:print(Json file doesn’t exist)print(json_dict):print(Error parsing json:, sys.exc_info()[1]).

Exit: If Hello.json exists

Name: Aanisha, last name: Mishra}

Exit: If Hello.json doesn’t exist

There was a mistake in taking Jason apart: Errno 2] There is no such file or directory: hello.json.

Conclusion: if the hello.json file is not a valid Json file.

The Json file doesn’t exist. Error
occurred during the Json analysis: the name json_dict is not defined.

This form of resolution makes it possible to deal with exceptions that are locked into the frame or forward.

More detailed treatment strategies can be found here.

If you like this article, you can also…


 python try except example,python try except any error

About Author