filmov
tv
Python Errors | Python Syntax | Types of Errors and Exceptions in python | Raising errors in Python

Показать описание
Python Errors | Types of Errors in python |
#Python_errors #Types_of_errors_in_Python
🖥️ Exceptions in Python: Types of Errors in Python (Syntax Errors, Logical Errors)
You can make certain mistakes while writing a program that lead to errors, when you try to run it. During its execution, Python program terminates as soon as it encounters an error, that is not handled. These errors can be classified into 2 classes:
- Syntax errors in Python
- Logical errors in Python - also known as Exceptions
○ Syntax Errors in Python
Errors caused by not following the proper structure of the language, are called syntax error - or parsing error. Syntax errors are the most basic type of error. They arise when the Python parser is unable to understand a line of code. Most syntax errors are: typos, incorrect indentation, or incorrect arguments. If you get this error, try looking at your code for any of these: misspelling, missing, or misusing keyword. An arrow indicates where the parser ran into the syntax error.
for action in ["like", "comment", "share"]
^
SyntaxError: invalid syntax
○ Logical Errors in Python (Exceptions)
Errors that occur at runtime - after passing the syntax test - are called Exceptions in Python, or Logical errors in Python. These errors are the most difficult type of errors to find because they will give unpredictable results and may crash your program.
A lot of different things can happen, if it encounters a Logical error. Whenever these types of runtime errors occur, Python will create an Exception object.
Exceptions arise when the python parser knows what to do with this piece of code, BUT is unable to perform this action. e.g Trying to access the Internet without internet connection: the Python interpreter knows what to do with that command, but is unable to perform it.
Illegal operations can raise Exceptions, too. Actually, there are plenty of built-in exceptions that are raised in Python, when the following corresponding errors occur: ValueError, TypeError, ZeroDivisionError, ImportError - and, so many more! Consequently, if not handled properly, Python prints the Traceback of that error. And the last line of this message indicates what type of exception error your program ran into - along with some details about what triggered this error.
for i in [1, 2, 3]:
print(i / 0)
Traceback (most recent call last):
File "stdin", line 2, in module
ZeroDivisionError: division by zero
Fortunately, you can catch and handle all these kind of exceptions, then also adapt your program so it does not crash. You can also define your own exceptions in Python, when required!
Everyone makes mistakes, and one of the most common Python mistakes is a syntax error, which is invalid code that the python interpreter doesn’t understand. The interpreter finds syntax errors before you are able to run the program, and gives you some hints on how to fix them. It takes some practice dealing with syntax errors, but eventually you should get the hang of it and fix them quickly.
Ex: print(name); # should not have a semicolon at the end
print(‘hello world”)
It’s good practice to just write a few lines of code, run it and verify that it works, then write a few more lines of code.
Runtime errors
A runtime error is an error that crashes your program while it is running. The Python interpreter does not detect a runtime error because the program's syntax is correct but the program attempts an illegal operation, such as dividing by zero or multiplying strings literals. When a runtime error happens, your program crashes and halts execution.
Ex:
print(‘hello’ * ‘world)
print(‘the end’)
Logic errors
Making a logical mistake in your code, such as multiplying when you should have added is a logic error. The interpreter doesn’t detect logic errors, and the program also doesn’t produce a runtime error and crash. Your program simply just produces the wrong output.
Ex:
sum = 3 * 4
print(‘the sum of 3 and 4 is’, sum)
Other common types of errors
Indentation error is when you didn’t properly indent your code. Ex:
print(‘why did I indent this?’)
Value error is when you pass the wrong value to a function such as int(). Ex:
int(‘five’) # cannot pass letters to int(), only number string literals
Name error is when the program tries to use a variable that does not exist. Ex: first_name = ‘Bob’
print(last_name)
Type error is when an incorrect type is used, such as adding a string to an integer. Ex:
age = 20 + " years old"
types of errors in python with examples,
how to catch all types of errors in python,
different types of errors in python,
what are the various types of errors in python,
#runtime_errors_in_python
#logical_errors_in_python
#python_error_types
#types_of_errors_in_python
#python_syntax_error
#python_runtime_error
#python_logical_error
#python_indenation_error
#python_name_error
#python_value_error
#Python_errors #Types_of_errors_in_Python
🖥️ Exceptions in Python: Types of Errors in Python (Syntax Errors, Logical Errors)
You can make certain mistakes while writing a program that lead to errors, when you try to run it. During its execution, Python program terminates as soon as it encounters an error, that is not handled. These errors can be classified into 2 classes:
- Syntax errors in Python
- Logical errors in Python - also known as Exceptions
○ Syntax Errors in Python
Errors caused by not following the proper structure of the language, are called syntax error - or parsing error. Syntax errors are the most basic type of error. They arise when the Python parser is unable to understand a line of code. Most syntax errors are: typos, incorrect indentation, or incorrect arguments. If you get this error, try looking at your code for any of these: misspelling, missing, or misusing keyword. An arrow indicates where the parser ran into the syntax error.
for action in ["like", "comment", "share"]
^
SyntaxError: invalid syntax
○ Logical Errors in Python (Exceptions)
Errors that occur at runtime - after passing the syntax test - are called Exceptions in Python, or Logical errors in Python. These errors are the most difficult type of errors to find because they will give unpredictable results and may crash your program.
A lot of different things can happen, if it encounters a Logical error. Whenever these types of runtime errors occur, Python will create an Exception object.
Exceptions arise when the python parser knows what to do with this piece of code, BUT is unable to perform this action. e.g Trying to access the Internet without internet connection: the Python interpreter knows what to do with that command, but is unable to perform it.
Illegal operations can raise Exceptions, too. Actually, there are plenty of built-in exceptions that are raised in Python, when the following corresponding errors occur: ValueError, TypeError, ZeroDivisionError, ImportError - and, so many more! Consequently, if not handled properly, Python prints the Traceback of that error. And the last line of this message indicates what type of exception error your program ran into - along with some details about what triggered this error.
for i in [1, 2, 3]:
print(i / 0)
Traceback (most recent call last):
File "stdin", line 2, in module
ZeroDivisionError: division by zero
Fortunately, you can catch and handle all these kind of exceptions, then also adapt your program so it does not crash. You can also define your own exceptions in Python, when required!
Everyone makes mistakes, and one of the most common Python mistakes is a syntax error, which is invalid code that the python interpreter doesn’t understand. The interpreter finds syntax errors before you are able to run the program, and gives you some hints on how to fix them. It takes some practice dealing with syntax errors, but eventually you should get the hang of it and fix them quickly.
Ex: print(name); # should not have a semicolon at the end
print(‘hello world”)
It’s good practice to just write a few lines of code, run it and verify that it works, then write a few more lines of code.
Runtime errors
A runtime error is an error that crashes your program while it is running. The Python interpreter does not detect a runtime error because the program's syntax is correct but the program attempts an illegal operation, such as dividing by zero or multiplying strings literals. When a runtime error happens, your program crashes and halts execution.
Ex:
print(‘hello’ * ‘world)
print(‘the end’)
Logic errors
Making a logical mistake in your code, such as multiplying when you should have added is a logic error. The interpreter doesn’t detect logic errors, and the program also doesn’t produce a runtime error and crash. Your program simply just produces the wrong output.
Ex:
sum = 3 * 4
print(‘the sum of 3 and 4 is’, sum)
Other common types of errors
Indentation error is when you didn’t properly indent your code. Ex:
print(‘why did I indent this?’)
Value error is when you pass the wrong value to a function such as int(). Ex:
int(‘five’) # cannot pass letters to int(), only number string literals
Name error is when the program tries to use a variable that does not exist. Ex: first_name = ‘Bob’
print(last_name)
Type error is when an incorrect type is used, such as adding a string to an integer. Ex:
age = 20 + " years old"
types of errors in python with examples,
how to catch all types of errors in python,
different types of errors in python,
what are the various types of errors in python,
#runtime_errors_in_python
#logical_errors_in_python
#python_error_types
#types_of_errors_in_python
#python_syntax_error
#python_runtime_error
#python_logical_error
#python_indenation_error
#python_name_error
#python_value_error