这是try-catch-finally的可接受使用吗?

时间:2017-11-11 05:55:33

标签: php error-handling

基本上我正在寻找一些澄清,这是一个可以接受的尝试,最后捕捉?基于电子邮件和用户名在表格中是唯一的

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"); 
    }       

1 个答案:

答案 0 :(得分:1)

不,你不能两次同样catch(Exception $e)。如果需要使用不同的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"];
        $errors[] = ["name" => "email", "error" => "Email may already be registered"];
    } 
    finally 
    { 
        header("refresh:10; url=index.php"); 
    }