Added try-except block for handling different direction types