def test_exception_during_execution(windows_bash_session):
    """Test handling of exceptions during command execution."""
    # Patch the PowerShell class itself within the module where it's used
    patch_target = 'openhands.runtime.utils.windows_bash.PowerShell'

    # Create a mock PowerShell class
    mock_powershell_class = MagicMock()
    # Configure its Create method (which is called in execute) to raise an exception
    # This simulates an error during the creation of the PowerShell object itself.
    mock_powershell_class.Create.side_effect = Exception(
        'Test exception from mocked Create'
    )

    with patch(patch_target, mock_powershell_class):
        action = CmdRunAction(command="Write-Output 'Test'")
        # Now, when execute calls PowerShell.Create(), it will hit our mock and raise the exception
        result = windows_bash_session.execute(action)

        # The exception should be caught by the try...except block in execute()
        assert isinstance(result, ErrorObservation)
        # Check the error message generated by the execute method's exception handler
        assert 'Failed to start PowerShell job' in result.content
        assert 'Test exception from mocked Create' in result.content
Test handling of exceptions during command execution.
tests/unit/test_windows_bash.py