Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit cd93480

Browse files
Add exception handling module with customizable settings and formatting example
1 parent 8f11e85 commit cd93480

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

example.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from exception.handler import Handler
2+
from exception.colors import Colors, Format
3+
4+
# ALL BOOLEAN SETTINGS ARE FALSE BY DEFAULT,
5+
# THE DEFAULT PRINT FUNCTION IS 'print()',
6+
# THE DEFAULT COLOR IS WHITE, DEFAULT FORMAT IS NORMAL.
7+
8+
# Initialize the Handler with custom settings - These are global settings
9+
# NOTE: return_string_rather_than_print has priority over exit_script both locally and globally!
10+
handler = Handler(
11+
show_line=True, # Show the line number where the exception occurred
12+
trace=True, # Include the traceback in the output
13+
use_timestamp=True, # Add a timestamp to the exception message
14+
exit_script=True, # Exit the script after handling the exception
15+
return_string_rather_than_print=True
16+
# Return the message instead of printing it,
17+
# if True it will override/ignore exit_script
18+
)
19+
20+
# Customize the formatter
21+
# NOTE: Specific settings always the defaults,
22+
# example if message_color is set it will override the main_color for the message part,
23+
handler.formatter(
24+
main_color=Colors.RED, # Set the main color to red
25+
message_color=Colors.YELLOW, # Set the message color to yellow
26+
trace_color=Colors.CYAN, # Set the traceback color to cyan
27+
timestamps_color=Colors.GREEN, # Set the timestamp color to green
28+
main_format=Format.BOLD, # Set the main format to bold
29+
message_format=Format.UNDERLINE, # Underline the message text
30+
trace_format=Format.DIM, # Dim the traceback text
31+
timestamps_format=Format.BLINK, # Blink the timestamp text
32+
datetime_format="%d-%m-%Y %H:%M:%S" # Customize the datetime format
33+
)
34+
35+
36+
# Example function to demonstrate exception handling
37+
def divide_numbers(a, b):
38+
try:
39+
return a / b
40+
except Exception as e:
41+
handler.exception(
42+
# These are local settings, if they are different from the global, they take priority
43+
# so they will override the global settings only for this case.
44+
msg=e, # Detailed exception message, can be a custom string
45+
exit_script=True, # Exit the script after handling the exception
46+
quit_code=1, # Exit code to use when exiting the script, defaults to 1
47+
return_string_rather_than_print=False # Return the message instead of printing it
48+
# NOTE: If return_string_rather_than_print is set to True globally,
49+
# and you want to locally quit for this singular case,
50+
# then you HAVE TO explicitly set it to False here.
51+
)
52+
53+
54+
# Trigger an exception
55+
divide_numbers(10, 0)
56+
print("Hello") # This line will not be executed if exit_script is True

0 commit comments

Comments
 (0)