- 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
- Mastering AWS Lambda
- Instant Apache Stanbol
- R語言數據可視化之美:專業圖表繪制指南
- Koa開發:入門、進階與實戰
- Web Application Development with MEAN
- Mastering Rust
- 零基礎輕松學SQL Server 2016
- Jenkins Continuous Integration Cookbook(Second Edition)
- HTML 5與CSS 3權威指南(第3版·上冊)
- Regression Analysis with Python
- .NET 4.0面向對象編程漫談:應用篇
- Microsoft HoloLens By Example
- Dart:Scalable Application Development
- JavaScript語法簡明手冊
- Apache Kafka 1.0 Cookbook