
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
What is exception handling in Python?
An error is something that goes wrong in the program at the compile time like a syntactical error.
For example.
'abe' = 5
OUTPUT
SyntaxError: can't assign to literal
Even if 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 and are not always fatal. If the exceptions are not handled error messages show up when the code is executed or run.
In general, when a Python script encounters an error that it can’t handle, it raises an exception and creates an exception object.
Usually, the script handles the exception immediately. If it doesn’t do so, then the program will terminate and print a traceback to the error along with its details. For example
abe < 5
OUTPUT
Traceback (most recent call last): File "C:/Users/TutorialsPoint1/~.py", line 1, in <module> abe < 5 NameError: name 'abe' is not defined
Exceptions are convenient in many ways for handling errors and special conditions in a program. When you think that you have a code which can produce an error then you can use exception handling.
- Related Articles
- What is exception handling in C#?
- What is Exception Handling in PHP ?
- What are the best practices for exception handling in Python?
- Exception handling in PowerShell
- Exception Handling Basics in C++
- Exception Handling in C++ vs Java
- NodeJS - Exception Handling in Asynchronous Code
- NodeJS - Exception Handling in eventful Code
- NodeJS - Exception Handling in Synchronous Code
- What is Exception in Python?
- PHP Exception Handling with finally
- C# Exception Handling Best Practices
- Exception handling and object destruction in C++
- Exception handling with method overriding in Java.
- Importance of Proper Exception Handling in Java
