Python Exception Handling
Catching except Python. In Python, but it is planned to use a clear statement. A major task that can enhance the exceptions is put in the intruder block and the code that deals with the exception of what is written about if it contains. ... If there was an IOException except for the block it kept eyes and regular flow.

Index
• What are the exceptions to Python?
• Python capture
• Conducting special pyramids 'python'
• Promote agencies
• try ... finally
What is Python?
Python has a lot of exceptions to the military forces that go to the pirate program when something goes wrong.
Apart from this, it is now done to stop the fixing system until it is up. If not, the program will be closed.
For example, if A work has called B work it also calls C function and except that it is connected to C. If he does not arrange for C, except the right to return to B of A.
If ever, a spam message and program comes suddenly, unexpectedly.
Python arrest
In Python, but it is planned to use a clear statement.
A major task that can enhance the exceptions is put in the intruder block and the code that deals with the exception of what is written about if it contains.
It all depends on the procedure, how to handle it when you have it, except it.
simple example.
# import module sys to get the type of exception
import sys
randomList = ['a', 0, 2]
for entry in randomList:
try:
print("The entry is", entry)
r = 1/int(entry)
break
except:
print("Oops!",sys.exc_info()[0],"occured.")
print("Next entry.")
print()
print("The reciprocal of",entry,"is",r)
Output:
The entry is a
Oops! <class 'ValueError'> occured.
Next entry.
The entry is 0
Oops! <class 'ZeroDivisionError' > occured.
Next entry.
The entry is 2
The reciprocal of 2 is 0.5
In this program, I loop until users enter into a legitimate two-dimensional code. The parts that can be excluded are placed in the attempted assault.
Unless otherwise, the block exception is removed and the normal flow continues. However, in the event of an exception, it is held in a special restraint.
Here, we write the name except for the use of ex_info () function in module sys and ask the user to try again. We can see that the 'a' and '1.3' value causes the ValueError and 0 to make ZeroDivisionError.
Exclusion of Python
In the previous example, I did not say, but the amount of exception.
The other culture of good programs because they take all the excuse and deal with each case in the same way. We can decide where to go but with the exception of the cost of the renewal.
A crush A can attempt to have a single number, a can be crushed apart, except for one, but only one will be killed unless otherwise exposed.
simple example.
try:
# do something
Pass
except ValueError:
# handle ValueError exception
Pass
except (TypeError, ZeroDivisionError):
# handle multiple exceptions
# TypeError and ZeroDivisionError
Pass
except:
# handle all other exceptions
pass
You can use a tuple of values to detect some exceptions that can be crushed except. Here is an example code for the password.
Raising Exceptions
In programs that were supposed to be excluded that when the error associated with the event events, but we strongly promote the use of keywords.
simple example.
>>> raise KeyboardInterrupt
Traceback (most recent call last):
...
KeyboardInterrupt
>>> raise MemoryError("This is an argument")
Traceback (most recent call last):
...
MemoryError: This is an argument
>>> try:
... a = int(input("Enter a positive integer: "))
... if a <= 0:
... raise ValueError("That is not a positive number!")
... except ValueError as ve:
... print(ve)
...
Enter a positive integer: -2
That is not a positive number!
We also optionally refer to the price exclusively to explain why, except that it has grown.
Try it out ... finally
Try Python phrases that have the option to end crop. This clause is executed no matter what and generally used to manufacture external resources.
For example, you can connect to a remote data center or file or interface user interface (GUI) interface.
In all cases, we need to clean up the resources once, that was a success or not. These actions (shut down a file, guinea or open network) are executed at the end of the process to ensure implementation.
simple example.
try:
f = open("test.txt",encoding = 'utf-8')
# perform file operation
finally:
f.close()
To getting expect level training for Python training in your Location –Python training in Chennai | Python training in Bangalore | Python training in Pune | Python training in OMR | Python training in Velachery | Python training in Tambaram | Python training in Annanagar | Python training in Chennai | Python training in Marathahalli | Python training in btm | Python Online Training.
Comments
Post a Comment