Unit V
Unit V
Unit V
Introduction
A Regular Expression or RegEx is a special sequence of characters that uses a search pattern
to find a string or set of strings. It can detect the presence or absence of a text by matching it
with a particular pattern and also can split a pattern into one or more sub-patterns.
Output
Start Index: 6
End Index: 7
Note: Here r character (r’portal’) stands for raw, not regex. The raw string is slightly
different from a regular string, it won’t interpret the \ character as an escape character. This
is because the regular expression engine uses \ character for its own escaping purpose.
Before starting with the Python regex module let’s see how to actually write regex using
metacharacters or special sequences.
Error in Python can be of two types i.e. Syntax errors and Exceptions. Errors are problems in a
program due to which the program will stop the execution. On the other hand, exceptions
are raised when some internal events occur which change the normal flow of the program.
Different types of exceptions in python:
In Python, there are several built-in Python exceptions that can be raised when an error occurs
during the execution of a program. Here are some of the most common types of exceptions
in Python:
SyntaxError: This exception is raised when the interpreter encounters a syntax error in
the code, such as a misspelled keyword, a missing colon, or an unbalanced parenthesis.
TypeError: This exception is raised when an operation or function is applied to an
object of the wrong type, such as adding a string to an integer.
NameError: This exception is raised when a variable or function name is not found in
the current scope.
IndexError: This exception is raised when an index is out of range for a list, tuple, or
other sequence types.
KeyError: This exception is raised when a key is not found in a dictionary.
ValueError: This exception is raised when a function or method is called with an
invalid argument or input, such as trying to convert a string to an integer when the
string does not represent a valid integer.
AttributeError: This exception is raised when an attribute or method is not found on
an object, such as trying to access a non-existent attribute of a class instance.
IOError: This exception is raised when an I/O operation, such as reading or writing a
file, fails due to an input/output error.
ZeroDivisionError: This exception is raised when an attempt is made to divide a
number by zero.
ImportError: This exception is raised when an import statement fails to find or load a
module.
These are just a few examples of the many types of exceptions that can occur in Python. It’s
important to handle exceptions properly in your code using try-except blocks or other error-
handling techniques, in order to gracefully handle errors and prevent the program from
crashing
Syntax Error: As the name suggests this error is caused by the wrong syntax in the
code. It leads to the termination of the program.
Example:
There is a syntax error in the code . The ‘if' statement should be followed by a
colon (:), and the ‘print' statement should be indented to be inside the ‘if' block.
amount = 10000
if(amount > 2999)
print("You are eligible to purchase Dsa Self Paced")
Output:
Exceptions: Exceptions are raised when the program is syntactically correct, but the
code results in an error. This error does not stop the execution of the program,
however, it changes the normal flow of the program.
Example:
Here in this code as we are dividing the ‘marks’ by zero so error will occur
known as ‘ZeroDivisionError’. ‘ZeroDivisionError’ occurs when we try to
divide any number by 0.
marks = 10000
a = marks / 0
print(a)
Output:
A try statement can have more than one except clause, to specify handlers for different
exceptions. Please note that at most one handler will be executed. For example, we can add
IndexError in the above code. The general syntax for adding specific exceptions are –
try:
# statement(s)
except IndexError:
# statement(s)
except ValueError:
# statement(s)
The code defines a function ‘fun(a)' that calculates b based on the input a. If a is less than 4, it
attempts a division by zero, causing a ‘ZeroDivisionError'. The code calls fun(3) and fun(5)
inside a try-except block. It handles the ZeroDivisionError for fun(3) and prints
“ZeroDivisionError Occurred and Handled.” The ‘NameError' block is not executed since
there are no ‘NameError' exceptions in the code.
def fun(a):
if a < 4:
b = a/(a-3)
print("Value of b = ", b)
try:
fun(3)
fun(5)
except ZeroDivisionError:
print("ZeroDivisionError Occurred and Handled")
except NameError:
print("NameError Occurred and Handled")
Output
The output above is so because as soon as python tries to access the value of b, NameError
occurs.
In Python, you can also use the else clause on the try-except block which must be present
after all the except clauses. The code enters the else block only if the try clause does not raise
an exception.
The code defines a function AbyB(a, b) that calculates c as ((a+b) / (a-b)) and handles a
potential ZeroDivisionError. It prints the result if there’s no division by zero error. Calling
AbyB(2.0, 3.0) calculates and prints -5.0, while calling AbyB(3.0, 3.0) attempts to divide by
zero, resulting in a ZeroDivisionError, which is caught and “a/b results in 0” is printed.
-5.0
a/b result in 0
Python provides a keyword finally, which is always executed after the try and except blocks.
The final block always executes after the normal termination of the try block or after the try
block terminates due to some exception. The code within the finally block is always
executed.
Syntax:
try:
# Some Code....
except:
# optional block
# Handling of exception (if required)
else:
# execute if no exception
finally:
# Some code .....(always executed)
Example:
Raising Exception
The raise statement allows the programmer to force a specific exception to occur. The sole
argument in raise indicates the exception to be raised. This must be either an exception
instance or an exception class (a class that derives from Exception).
This code intentionally raises a NameError with the message “Hi there” using
the raise statement within a try block. Then, it catches the NameError exception,
prints “An exception,” and re-raises the same exception using raise. This demonstrates
how exceptions can be raised and handled in Python, allowing for custom error messages
and further exception propagation.
try:
raise NameError("Hi there")
except NameError:
print ("An exception")
raise
The output of the above code will simply line printed as “An exception” but a Runtime error
will also occur in the last due to the raise statement in the last line. So, the output on your
command line will look like
Traceback (most recent call last):
File "/home/d6ec14ca595b97bff8d8034bbf212a9f.py", line 5, in <module>
raise NameError("Hi there") # Raise Error
NameError: Hi there
Improved program reliability: By handling exceptions properly, you can prevent your
program from crashing or producing incorrect results due to unexpected errors or input.
Simplified error handling: Exception handling allows you to separate error handling
code from the main program logic, making it easier to read and maintain your code.
Cleaner code: With exception handling, you can avoid using complex conditional
statements to check for errors, leading to cleaner and more readable code.
Increased code complexity: Exception handling can make your code more complex,
especially if you have to handle multiple types of exceptions or implement complex
error handling logic.
Possible security risks: Improperly handled exceptions can potentially reveal sensitive
information or create security vulnerabilities in your code, so it’s important to handle
exceptions carefully and avoid exposing too much information about your program.