refers to errors that occur when parsing the code. When the code does not comply with Python syntax rules, the Python interpreter will report a SyntaxError syntax error during parsing, and at the same time, it will clearly point out the earliest statement where the error was detected. For example:
print "Hello,World!"
Copy after login
We know that Python 3.0 no longer supports the above writing method, so at run time, the interpreter will report the following error:
SyntaxError: Missing parentheses in call to 'print'
Copy after login
Syntax error Most of them are caused by the developer's negligence. They are real errors and cannot be tolerated by the interpreter. Therefore, the program can only be executed if all grammatical errors in the program are corrected.
2 | Run-time error
Run-time error means that the program is syntactically correct, but an error occurs during runtime. For example:
a = 1/0
Copy after login
The above code means "divide 1 by 0 and assign it to a. Because 0 is meaningless as a divisor, the following error will occur after running:
Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> 1/0 ZeroDivisionError: division by zero
Copy after login
3 |Other exceptions
In the above running output results, the first two paragraphs indicate the location of the error, and the last sentence indicates the type of error. In Python, This kind of error situation during runtime is called Exceptions.
There are many such exceptions. The common exceptions are as follows:
Exception type
Meaning
Instance
AssertionError
When assert keyword When the condition is false, the program will stop and throw this exception
##>>> assert 1>0>>> assert 1<0AssertionError
AttributeError
Exception thrown when the object attribute attempted to be accessed does not exist
>>> s="hello">>> s.lenAttributeError: 'str' object has no attribute'len'
IndexError
The index exceeds the range of the sequence and this exception will be thrown
>>> s="hello">>> s[5]IndexError: string index out of range
KeyError
Search in dictionary This exception is thrown when a keyword does not exist
try:
a = int(input("输入被除数:"))
b = int(input("输入除数:"))
c = a / b
print("您输入的两个数相除的结果是:", c )
except (ValueError, ArithmeticError):
print("程序发生了数字格式异常、算术异常之一")
except :
print("未知异常")
print("程序继续运行")
The above is the detailed content of What are the methods for catching and handling exceptions in Python. For more information, please follow other related articles on the PHP Chinese website!
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.
HadiDB: A lightweight, high-level scalable Python database HadiDB (hadidb) is a lightweight database written in Python, with a high level of scalability. Install HadiDB using pip installation: pipinstallhadidb User Management Create user: createuser() method to create a new user. The authentication() method authenticates the user's identity. fromhadidb.operationimportuseruser_obj=user("admin","admin")user_obj.
It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).
MySQL database performance optimization guide In resource-intensive applications, MySQL database plays a crucial role and is responsible for managing massive transactions. However, as the scale of application expands, database performance bottlenecks often become a constraint. This article will explore a series of effective MySQL performance optimization strategies to ensure that your application remains efficient and responsive under high loads. We will combine actual cases to explain in-depth key technologies such as indexing, query optimization, database design and caching. 1. Database architecture design and optimized database architecture is the cornerstone of MySQL performance optimization. Here are some core principles: Selecting the right data type and selecting the smallest data type that meets the needs can not only save storage space, but also improve data processing speed.
Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.
As a data professional, you need to process large amounts of data from various sources. This can pose challenges to data management and analysis. Fortunately, two AWS services can help: AWS Glue and Amazon Athena.
You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.
No, MySQL cannot connect directly to SQL Server. But you can use the following methods to implement data interaction: Use middleware: Export data from MySQL to intermediate format, and then import it to SQL Server through middleware. Using Database Linker: Business tools provide a more friendly interface and advanced features, essentially still implemented through middleware.