Errors & Exceptions
There are two major kinds of errors:
- Syntax Errors
- Exceptions
Syntax Errors
Syntax errors are parsing errors which occur when the code is not adhering to Python Syntax.
Code
Output
When there is a syntax error, the program will not execute even if that part of code is not used.
Code
Output
Notice that in the above code, the syntax error is inside the
Exceptions
Even when a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it.
Errors detected during execution are called exceptions.
Example Scenario
We wrote a program to download a Video over the Internet.
- Internet is disconnected during the download
We do not have space left on the device to download the video
Example 1
Division Example
Input given by the user is not within expected values.
Code
Output
Example 2
Input given by the user is not within expected values.
Code
Output
Example 3
Consider the following code, which is used to update the quantity of items in store.
Code
Output
Working With Exceptions
What happens when your code runs into an exception during execution?
The application/program crashes.
End-User Applications
When you develop applications that are directly used by end-users, you need to handle different possible exceptions in your code so that the application will not crash.
Reusable Modules
When you develop modules that are used by other developers, you should raise exceptions for different scenarios so that other developers can handle them.
Money Transfer App Scenario
Let’s consider we are creating an app that allows users to transfer money between them.
Bank Account Class
Example 1
Code
Output
Example 2
Code
Output
Raising Exceptions
When your code enters an unexpected state, raise an exception to communicate it.
Built-in Exceptions
Different exception classes which are raised in different scenarios.
You can use the built-in exception classes with raise keyword to raise an exception in the program.
Code
We can pass message as argument .
Output
Bank Account Class
Example 1
Code
Output
Handling Exceptions
Python provides a way to catch the exceptions that were raised so that they can be properly handled.
- Exceptions can be handled with try-except block.
Whenever an exception occurs at some line in try block, the execution stops at that line and jumps to except block.
PYTHON
Transfer Amount
Example 1
Code
Output
Summary
Reusable Modules
While developing reusable modules, we need to raise Exceptions to stop our code from being used in a bad way.
End-User Applications
While developing end-user applications, we need to handle Exceptions so that application will not crash when used.
Handling Specific Exceptions
We can specifically mention the name of exception to catch all exceptions of that specific type.
Syntax
Example 1
Code
Input
Output
Example 2
Code
Input given by the user is not within expected values.
Input
Output
We can also access the handled exception in an object.
Syntax
Code
Output
Handling Multiple Exceptions
We can write multiple exception blocks to handle different types of exceptions differently.
Syntax
Example 1
Code
Input
Output
Example 2