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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.