Skip to content

Instantly share code, notes, and snippets.

@Rorythedev
Created September 21, 2024 16:23
Show Gist options
  • Save Rorythedev/ead05fd7fc0377dbc91836eec79c3393 to your computer and use it in GitHub Desktop.
Save Rorythedev/ead05fd7fc0377dbc91836eec79c3393 to your computer and use it in GitHub Desktop.
prevents your game from crashing. WARNING: This is in beta and may contain bugs and glitches
def handle_game_crash(func):
"""
Decorator function to handle and gracefully handle game crashes.
This decorator function wraps a game-related function and catches any
exceptions that may occur during its execution. It then logs the error
and provides a friendly message to the user, preventing the game from
crashing.
Args:
func (callable): The game-related function to be wrapped.
Returns:
callable: The wrapped function that handles exceptions.
"""
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
# Log the error
print(f"Oops! An unexpected error occurred: {e}")
print("Don't worry, we've got your back! The game will continue without any issues.")
# Gracefully handle the exception
return None
return wrapper
# Example usage
@handle_game_crash
def play_game():
# Game logic goes here
some_value = 1 / 0 # This will intentionally raise a ZeroDivisionError
return "Game completed successfully!"
result = play_game()
if result:
print(result)
else:
print("Looks like the game didn't complete as expected. Let's try again!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment