- Mastering Python Scripting for System Administrators
- Ganesh Sanjiv Naik
- 420字
- 2021-07-02 14:00:31
Error handling (exception handling)
In this section, we're going to learn how Python handles exceptions. But first, what is an exception? An exception is an error that occurs during program execution. Whenever any error occurs, Python generates an exception that will be handled using a try…except block. Some exceptions can't be handled by programs so they result in error messages. Now, we are going to see some exception examples.
In your Terminal, start the python3 interactive console and we will see some exception examples:
student@ubuntu:~$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> 50 / 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>>
>>> 6 + abc*5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'abc' is not defined
>>>
>>> 'abc' + 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't convert 'int' object to str implicitly
>>>
>>> import abcd
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'abcd'
>>>
These are some examples of exceptions. Now, we will see how we can handle the exceptions.
Whenever errors occur in your Python program, exceptions are raised. We can also forcefully raise an exception using raise keyword.
Now we are going to see a try…except block that handles an exception. In the try block, we will write a code that may generate an exception. In the except block, we will write a solution for that exception.
The syntax for try…except is as follows:
try:
statement(s)
except:
statement(s)
A try block can have multiple except statements. We can handle specific exceptions also by entering the exception name after the except keyword. The syntax for handling a specific exception is as follows:
try:
statement(s)
except exception_name:
statement(s)
We are going to create an exception_example.py script to catch ZeroDivisionError. Write the following code in your script:
a = 35
b = 57
try:
c = a + b
print("The value of c is: ", c)
d = b / 0
print("The value of d is: ", d)
except:
print("Division by zero is not possible")
print("Out of try...except block")
Run the script as follows and you will get the following output:
student@ubuntu:~$ python3 exception_example.py
The value of c is: 92
Division by zero is not possible
Out of try...except block
- Cocos2d Cross-Platform Game Development Cookbook(Second Edition)
- Visual C++程序設計學習筆記
- NLTK基礎教程:用NLTK和Python庫構建機器學習應用
- 程序員數(shù)學:用Python學透線性代數(shù)和微積分
- 深度學習:算法入門與Keras編程實踐
- Unreal Engine 4 Shaders and Effects Cookbook
- C#實踐教程(第2版)
- AIRIOT物聯(lián)網(wǎng)平臺開發(fā)框架應用與實戰(zhàn)
- Webpack實戰(zhàn):入門、進階與調(diào)優(yōu)
- Practical Microservices
- 現(xiàn)代C:概念剖析和編程實踐
- Oracle 12c從入門到精通(視頻教學超值版)
- 3D Printing Designs:Octopus Pencil Holder
- C語言程序設計教程
- 讀故事學編程:Python王國歷險記