Python Programming/Errors

In python there are three types of errors; syntax errors, logic errors and exceptions.

Syntax errors
Syntax errors are the most basic type of error. They arise when the Python parser is unable to understand a line of code. Syntax errors are almost always fatal, i.e. there is almost never a way to successfully execute a piece of code containing syntax errors. Some syntax errors can be caught and handled, like eval(""), but these are rare.

In IDLE, it will highlight where the syntax error is. Most syntax errors are typos, incorrect indentation, or incorrect arguments. If you get this error, try looking at your code for any of these.

Logic errors
These are the most difficult type of error to find, because they will give unpredictable results and may crash your program. A lot of different things can happen if you have a logic error. However these are very easy to fix as you can use a debugger, which will run through the program and fix any problems.

A simple example of a logic error can be showcased below, the while loop will compile and run however, the loop will never finish and may crash Python:

Logic errors are only erroneous in the perspective of the programming goal one might have; in many cases Python is working as it was intended, just not as the user intended. The above while loop is functioning correctly as Python is intended to, but the exit condition the user needs is missing.

Exceptions
Exceptions arise when the python parser knows what to do with a piece of code but is unable to perform the action. An example would be trying to access the internet with python without an internet connection; the python interpreter knows what to do with that command but is unable to perform it.

Dealing with exceptions
Unlike syntax errors, exceptions are not always fatal. Exceptions can be handled with the use of a try statement.

Consider the following code to display the HTML of the website 'example.com'. When the execution of the program reaches the try statement it will attempt to perform the indented code following, if for some reason there is an error (the computer is not connected to the internet or something) the python interpreter will jump to the indented code below the 'except:' command.

Another way to handle an error is to except a specific error.

If the user enters a numeric value as his/her age, the output should look like this:

Enter your age: 5 Your age must be 5 years old.

However, if the user enters a non-numeric value as his/her age, a ValueError is thrown when trying to execute the int method on a non-numeric string, and the code under the except clause is executed:

Enter your age: five Your age must be numeric.

You can also use a try block with a while loop to validate input:

The program will prompt you for your age until you enter a valid age:

Enter your age: five Your age must be numeric. Enter your age: abc10 Your age must be numeric. Enter your age: 15 You must be 15 years old.

In certain other cases, it might be necessary to get more information about the exception and deal with it appropriately. In such situations the except as construct can be used.

enter the name of the file:file1.txt enter the name of the link:AlreadyExists.txt an error occurred linking file1.txt to AlreadyExists.txt: File exists error no 17

enter the name of the file:file1.txt enter the name of the link:/Cant/Write/Here/file1.txt an error occurred linking file1.txt to /Cant/Write/Here/file1.txt: Permission denied error no 13

Python/Conceitos básicos/Erros e exceções