Jump to content

Usage of Catch/Block


followInTo

Recommended Posts

So I'm debating whats the best method. I'm aware that catch/blocks should be used in exceptional cases but what if it saves me from having 4 nested if/else conditions? The following is code for an xbox registration site

try {
    // check code is valid
    $c1 = $this->input->post('code1',true);
    $c2 = $this->input->post('code2',true);
    $c3 = $this->input->post('code3',true);
    $gt = $this->input->post('gamertag',true);
    $pass = $this->input->post('password',true);
    if(
    $c1 == '' || $c2 == '' || $c3 == '') {
        throw new FormError("Authorization code invalid.");
    }
    // check db for code validation
    $q = $this->db->query("select * from `codes` where `activated` = 0 and `code` = ".$c1.$c2.$c3." limit 0,1");
    if($q->num_rows() != 1) {
        throw new FormError("Authorization code invalid.");
    }
    $r = $q->row_array();
    $email = $r["recipient_email"];
    $codeGt = $r["gamertag"];
    // check valid gamertag
    $q = $this->db->query("select * from `users` where `gamertag` = '".$gt."'");
    if($q->num_rows() > 0) {
        throw new FormError("Gamertag already exists.");
    }
    // validate gamertag /w inputted gamertag
    if(strtolower($codeGt) != strtolower($gt)) {
        throw new FormError("Gamertag Invalid");	
    }
    $this->_createAccount($email,$pass,$gt);
    // show register-thanks screen
    $this->load->view("register-thanks",$data);
    $success = true;
} catch (Exception $e) {
    $data['error'] = $e->getMessage();
}

Link to comment
https://forums.phpfreaks.com/topic/245614-usage-of-catchblock/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.