这是尝试捕获最终的可接受用法吗?



基本上我正在寻找一些澄清,这是try,catch的可接受用法吗?基于电子邮件和用户名在表中是唯一的

try
    {
        $db = new Database;
        $success = ["message" => "Please check your Email address to activate your account"];   
        $process = $db->prepare('INSERT INTO users (username, email, password,  profileImg, profileImgPath, profileImgType, accountStatus, verified, joined)
        VALUES
        (:username, :email, :password, :filename, :filepath, :filetype, :activationCode, 0, NOW())');
        $process->bindValue(':username', $username);
        $process->bindValue(':email', $email);
        $process->bindValue(':password', password_hash($post['password'], PASSWORD_DEFAULT));
        $process->bindValue(':activationCode', $activationCode);
        $process->bindValue(':filename', $filename);
        $process->bindValue(':filepath', $filepath);
        $process->bindValue(':filetype', $filetype);
        $process->execute();
        $code = 'https://gotsocial.co.uk/gotsocial/active.php?activecode=' . $activationCode . '. 
            ';
        $to = $post['email'];
        $subject = 'GOT Social';
        $from = "register@gotsocial.co.uk";
        $result = mail($to, $subject, $code, "From: $from");
    }
    catch(Exception $e)
    {
        $errors[] = ["name" => "username", "error" => "Username may already be taken"];
    }
    catch(Exception $e) 
    {
        $errors[] = ["name" => "email", "error" => "Email may already be registered"];
    } 
    finally 
    { 
        header("refresh:10; url=index.php"); 
    }       

不,你不能有两次相同的catch(Exception $e)。如果您需要不同的捕获,则必须更改异常的类型,否则必须将它们合并为一个

try
    {
        $db = new Database;
        $success = ["message" => "Please check your Email address to activate your account"];   
        $process = $db->prepare('INSERT INTO users (username, email, password,  profileImg, profileImgPath, profileImgType, accountStatus, verified, joined)
        VALUES
        (:username, :email, :password, :filename, :filepath, :filetype, :activationCode, 0, NOW())');
        $process->bindValue(':username', $username);
        $process->bindValue(':email', $email);
        $process->bindValue(':password', password_hash($post['password'], PASSWORD_DEFAULT));
        $process->bindValue(':activationCode', $activationCode);
        $process->bindValue(':filename', $filename);
        $process->bindValue(':filepath', $filepath);
        $process->bindValue(':filetype', $filetype);
        $process->execute();
        $code = 'https://gotsocial.co.uk/gotsocial/active.php?activecode=' . $activationCode . '. 
            ';
        $to = $post['email'];
        $subject = 'GOT Social';
        $from = "register@gotsocial.co.uk";
        $result = mail($to, $subject, $code, "From: $from");
    }
    catch(Exception $e)
    {
        $errors[] = ["name" => "username", "error" => "Username may already be taken"];
        $errors[] = ["name" => "email", "error" => "Email may already be registered"];
    } 
    finally 
    { 
        header("refresh:10; url=index.php"); 
    }       

最新更新