Errors and Exceptions

👋 Hi there, I am Nirmal. I am a former software engineer with a keen interest in data science and analytics domains. Besides, I love to contribute to open source and help others to understand various tech stuffs.
Search for a command to run...

👋 Hi there, I am Nirmal. I am a former software engineer with a keen interest in data science and analytics domains. Besides, I love to contribute to open source and help others to understand various tech stuffs.
No comments yet. Be the first to comment.
This series will provides the core concepts of python programming starting from variables, data types, control flow, and functions to some problem solving.
Python, a popular programming language, offers a powerful feature called object-oriented programming (OOP). This approach allows developers to organize their code in a way that makes it easier to understand and reuse. Central to OOP in Python are c...
Bias in Machine Learning Bias refers to the simplifying assumptions made by a model to make the target function easier to learn. High bias can lead to underfitting Represents the error introduced by approximating a real-world problem Example: A li...
When it comes to developing software applications, choosing the appropriate architectural style is a critical decision. Among the popular options are microservices and monolithic architecture. By understanding the trade-offs, readers can make well-i...

In a world where information is everywhere, businesses have tons of data at their fingertips. This data is like a goldmine waiting to be used for smart decisions. That's where Big Data analytics and Data Science come in. They're like the experts who ...

Python is a powerful tool for data professionals, largely due to its extensive collection of open-source libraries and packages. In this blog, we'll explore the fundamentals of pandas, with a special focus on its core data structures: Series and Data...

NumPy is a powerful library for numerical computing in Python, offering robust support for large, multi-dimensional arrays and an extensive collection of mathematical functions. Creating Arrays To begin using NumPy, import the library and create arra...

Errors and Exceptions in Python are mechanisms for handling issues that arise during program execution. Errors indicate code issues that prevent successful execution, while exceptions are events that disrupt the normal flow of code.
Errors where the code is not valid in Python.
a, b = 1, 2
if a < b
print("Messge")
Output :
File "main.py", line 2
if a < b
^
SyntaxError: invalid syntax
Semantic errors can lead to incorrect program behavior or unexpected results, even if the code runs without any exceptions
# Calculate the area of a circle
radius = 5
area = 3.14 * radius * radius
print(area)# output = 78.5
In this code snippet, the intention is to calculate the area of a circle using the formula pi * radius * radius. However, there is a semantic error in the calculation. Instead of using the accurate value of π (pi), an approximation of 3.14 is used.
import math
radius = 5
area = math.pi * radius * radius
print(area)# output = 78.53981633974483
Runtime errors are errors that occur while executing code at runtime.
They are caused by issues like dividing by zero, using undefined variables, performing operations on incompatible types, accessing invalid list indices, etc.
You can handle runtime errors using try/except blocks.
The try block contains the code that may raise an error, and the except block handles the error.
To catch a ZeroDivisionError:
try:
num / 0
except ZeroDivisionError:
print("Cannot divide by zero")
Catching TypeError:
num = 5
name = "nirmal"
try:
value = num + name
except TypeError:
print("Incompatible types")
To catch an IndexError:
try:
list[invalid_index]
except IndexError:
print("Invalid list index")
In addition to try and except, you can use the else and finally keywords to further tune your code’s handling of exceptions.
else: The optional else block is executed if no exceptions occur in the try block. It is used to specify code that should run only when no exceptions are raised.
finally: The optional finally block is always executed, regardless of whether an exception occurred or not. It is used to specify code that should run regardless of the outcome, such as cleanup operations or resource release.
try:
print("write some logic")
except:
print("this happens only if it fails")
else:
print("this happens only if it succeeds")
finally:
print("this happens no matter what")
In python, raise keyword is used to explicitly raise an exception or an error during the execution of a program.
It allows you to create custom exceptions or raise built-in exceptions to handle specific scenarios or error conditions.
Syntax : raise ExceptionType("Error message")
n = int(input("Enter any value::"))
if n < 0:
raise ValueError("Value cannot be negative")
else:
print(f"Enter value is :: {n}")