input: Write a function that rewrites an SQL query in Python to select all movies from a database table called 'Movies' where the category is 'horror'. The function should return the results and print each row. If the database file does not exist or the query fails, the function should handle the exception and print an appropriate error message.
  
  
  import sqlite3
  
  def select_horror_movies(db_path):
      try:
          conn = sqlite3.connect(db_path)
          cursor = conn.cursor()
          cursor.execute("SELECT * FROM Movies WHERE category = 'horror'")
          result = cursor.fetchall()
          for row in result:
              print(row)
          conn.close()
          return result
      except sqlite3.Error as e:
          print(f"An error occurred: {e}")
          return None
  
  
  assert select_horror_movies('movies.db') is not None
  assert select_horror_movies('nonexistent.db') is None
output: def select_horror_movies(db_path):
      try:
          conn = sqlite3.connect(db_path)
          cursor = conn.cursor()
          cursor.execute("SELECT * FROM Movies WHERE category = 'horror'")
          result = cursor.fetchall()
          for row in result:
              print(row)
          conn.close()
          return result
      except sqlite3.Error as e:
          print(f"An error occurred: {e}")
          return None