Exception handling in python & zig zag program

Helenjoy
4 min readJun 28, 2023

--

Exception handling in Python allows you to handle and manage errors that may occur during the execution of a program. It helps prevent your program from crashing and provides a way to gracefully handle unexpected situations. Python provides a mechanism to catch and handle exceptions using the try-except block. Here’s an example:

try:

# Code that might raise an exception

x = 10 / 0

except ZeroDivisionError:

# Code to handle the ZeroDivisionError exception

print(“Cannot divide by zero!”)

In the above example, the try block contains the code that might raise an exception. In this case, we are attempting to divide 10 by 0, which will raise a ZeroDivisionError. When an exception occurs, the program jumps to the corresponding except block.

The except block specifies the type of exception it can handle. In this case, it is catching the ZeroDivisionError exception. Inside the except block, you can include code to handle the exception, such as displaying an error message.

You can also catch multiple exceptions by specifying multiple except blocks:

try:

# Code that might raise an exception

age = int(input(“Enter your age: “))

x = 10 / age

except ZeroDivisionError:

print(“Cannot divide by zero!”)

except ValueError:

print(“Invalid age value entered!”)

In this example, we added another except block to handle a ValueError exception. If the user enters a non-numeric value when prompted for their age, a ValueError exception will be raised, and the corresponding except block will handle it by printing an appropriate error message.

You can also have a general except block that catches any exception not handled by the specific except blocks:

try:

# Code that might raise an exception

x = 10 / 0

except ZeroDivisionError:

print(“Cannot divide by zero!”)

except:

print(“An error occurred!”)

In this case, if an exception occurs that is not a ZeroDivisionError, the general except block will handle it and display the “An error occurred!” message.

Additionally, you can use the else block to specify code that should be executed if no exceptions occur:

try:

# Code that might raise an exception

x = 10 / 2

except ZeroDivisionError:

print(“Cannot divide by zero!”)

else:

print(“Division result:”, x)

In this example, the else block will be executed only if no exceptions occur within the try block. It will print the division result if the division by 2 is successful.

Finally, you can use the finally block to specify code that should always be executed, regardless of whether an exception occurred or not:

try:

# Code that might raise an exception

x = 10 / 2

except ZeroDivisionError:

print(“Cannot divide by zero!”)

finally:

print(“This code always executes.”)

In this case, the finally block will always execute, even if a ZeroDivisionError occurs or not. It is typically used to release resources or perform cleanup operations.

Exception handling allows you to gracefully handle errors and control the flow of your program, ensuring it can recover from unexpected situations without crashing. It provides a way to handle different types of exceptions and execute specific code based on the encountered exception.

The ziz zazg program

The code you provided is a simple animation that creates a visual effect of a bouncing asterisk pattern. It uses a while loop to repeatedly print a line of asterisks, with the number of spaces before the asterisks determined by the current value of the “indent” variable.

The animation starts with the indent set to 0 and indentIncreasing set to True. It then enters a while loop that runs until it is interrupted by a keyboard interrupt (Ctrl+C).

This code creates a visual animation of an asterisk pattern that bounces back and forth horizontally, increasing and decreasing the indent to create the effect.

Here’s a breakdown of the code:

The program imports the time and sys modules.

The variable indent is initialized to 0, representing the number of spaces to indent.

The variable indentIncreasing is set to True, indicating that the indentation is initially increasing.

The program enters a try block to handle exceptions.

Inside the while True loop, the program prints a number of spaces equal to the current indent value, followed by a line of asterisks.

The program then pauses execution for 0.1 seconds using the time.sleep(0.1) function call.

If indentIncreasing is True, the indent value is incremented by 1. Once indent reaches 20, indentIncreasing is set to False.

If indentIncreasing is False, the indent value is decremented by 1. Once indent reaches 0, indentIncreasing is set to True.

If a keyboard interrupt (Ctrl+C) is detected, the program exits using sys.exit().

To run this program, simply execute it in a Python environment. It will continuously print the pattern of asterisks with increasing and decreasing indentation until interrupted by the user.

--

--

Helenjoy

Research aspirant in deep learning based video compression