What are the potential pitfalls of setting session variables after a method call that may throw an exception in PHP?

Setting session variables after a method call that may throw an exception in PHP can lead to the session variables being set even if the method call fails, resulting in potentially incorrect or inconsistent session data. To solve this issue, session variables should be set before the method call that may throw an exception.

// Set session variables before calling method that may throw an exception
$_SESSION['key'] = 'value';

try {
    // Call method that may throw an exception
    someMethod();
} catch (Exception $e) {
    // Handle exception
}