Errors and exceptions in Python and how to fix them
4 minute(s) read | Published on: Oct 15, 2021 Updated on: Dec 14, 2021 |
Syntactic errors in Python
It should be noted that syntactic errors are known as parsing and are one of the most common errors that we may encounter when programming with the Python programming language in the design and development of applications and websites ( in French: conception et développement d'applications et de sites web ).
Like the following:
The code listed above has an error and repeats that error. This error displays a small arrow pointing to the first point on the line where the error was detected. In the example above, the error is displayed in print () function. The reason for this said error is that one of the (:) s has been deleted.
Exceptions in the programming language
You may have encountered the condition that a phrase or command and code from a spell checker and syntax is correct, but you will discover an error while executing it. Some errors when running a program are called exceptions while the phrase is spelling correct.
Like the following:
In the example we gave you, the error is ZeroDivisionError, NameError, and TypeError. In the last line of the error message, the exception type is also inserted so that the exception in this example is the built-in exception.
How can we manage exceptions in the programming language?
To manage some of the exceptions in the program, we can use programming.
Below is the code that has the KeyboardInterrupt exception.
The conditional try phrase in the code above works as follows:
- In the first step, the try section is executed.
- If no exceptions occur during execution, the except section will work.
- But if there is an exception while running the try section, other sections will not be executed. If the type of error and the name of the error occurred after the keyword is displayed and it is equal to except, the except section will be executed.
- If an exception has occurred and its name is not like the name of the exception in the except section, it will be transferred to the phrase outside try.
The class inside the except section is compatible with an exception.
The code below is the result and output of B, C, D.
Of course, it should be noted that if the except part is inverted, the output and the result will be B, B, B.
The conditional statement tries… except has an optional else that must follow all exceptions if it exists. This expression can be used for code that the try section has declared no exceptions.
Like the following:
It is better to use the else section instead of adding additional code to the try section. Because if you use it, it prevents you from getting any other code.
If an exception argues, messages will be printed for unmanaged exceptions.
Handling runtime error: division by zero
How can they be announced?
When making exceptions, we use some commands and phrases to declare them. For example, in using the phrase raise, the programmer will be safe until a specific exception occurs.
Like the following:
Only the raised argument is used to express the exception.
If an exception has been declared, but you do not wish to manage it, you will be able to use the phrase raise until you declare the exception again.
Make defined them
Exceptions are derived directly or indirectly through the Exception class. Exception classes can be defined so that the work of other classes can be done as well.
The try statement has another optional part that is responsible for clean-up actions.
- If an exception occurs during the trial run, the resulting exception may be managed by exception. If the exception is handled by except, it will be announced again after the exception is finally implemented.
- Exceptions can occur when executing except or else. After execution, the final section will be announced again.
- If the try statement reaches the break, continue, or return messages, the final section will be executed before the comments, continue, or return.
- If the final section has the expression return, the value of the data return will be from the return statement of the last paragraph.
Like the following:
In the codes listed above, the final section is always running.
This article addresses some of the errors and exceptions while coding. In addition to mistakes, some exceptions may occur. Exceptions are the same errors that occur at runtime. If you encounter these errors, you can use the contents of this article to identify them and the reason for their occurrence, then try to fix them.
About our Android and iOS app development services