try catchの用途が適切であるかわかりません。
PHPでアプリケーション開発を行う際、try catchをほぼ使います。
try catchがどのような動きをするかはわかっています。
下記はCodeIgniterのコントローラなのですが、バリデーションエラー等を全てthrowしてcatch内でエラー用の画面を表示させたりリダイレクトで飛ばしたりするような設計で開発してしまいます。
オープンソースのコード等を読んでいると、この様にtry catchを使っているコードをあまり見た事がありません。
エラー時の処理が楽だし見やすいと思っているのでこの様な書き方をしているのですが
何か問題点があるのでしょうか?
また、参考記事のC++だとパフォーマンスが悪くなるみたいな
他言語の情報でもご教授頂けますと幸いでございます。
参考記事:http://www.thinkridge.com/modules/tinyd1/content/index.php?id=7
php
1<?php 2defined('BASEPATH') OR exit('のび太さんのエッチ!'); 3class Api extends CI_Controller{ 4 function __construct() 5 { 6 parent::__construct(); 7 $this->load->model('api_model'); 8 $this->load->library('form_validation'); 9 } 10 11 public function add_user() 12 { 13 //POST Request以外は無視 14 if ( $this->input->method(TRUE) !== 'POST' ) return show_404(); 15 $json_str = $this->input->raw_input_stream; 16 //ここform_validationしやすい様に配列で渡す。 17 $json_data = json_decode($json_str, TRUE); 18 if ( empty($json_data) ) return show_404(); 19 20 $this->form_validation->set_data($json_data); 21 $this->form_validation->set_rules('name', 'lang:name', 'required|max_length[100]'); 22 $this->form_validation->set_rules('tel', 'lang:tel', 'required|is_natural|max_length[13]'); 23 $this->form_validation->set_rules('sex', 'lang:sex', 'required|in_list[1,2]'); 24 25 $result = new stdClass; 26 try 27 { 28 if ( $this->form_validation->run() === FALSE ) 29 { 30 throw new Exception('Request Validation Error', 2); 31 } 32 33 if ( ! $this->api_model->add_user($json_data) ) 34 { 35 throw new Exception('User add Error', 1); 36 } 37 38 $result->statusCode = 'success!!!'; 39 $result->message = 'ユーザー登録できたっぽいよ!'; 40 } 41 catch ( Exception $e ) 42 { 43 $result->statusCode = 'EA' . $e->getCode(); 44 $result->message = $e->getMessage(); 45 46 if ( ! empty(validation_errors()) ) 47 { 48 $result->validationMessages = $this->form_validation->error_array(); 49 } 50 } 51 return $this->output 52 ->set_content_type('application/json') 53 ->set_output(json_encode($result)); 54 } 55}
大げさですがifで分岐するパターンです。
php
1<?php 2defined('BASEPATH') OR exit('のび太さんのエッチ!'); 3class Api extends CI_Controller{ 4 function __construct() 5 { 6 parent::__construct(); 7 $this->load->model('api_model'); 8 $this->load->library('form_validation'); 9 } 10 11 public function add_user() 12 { 13 //POST Request以外は無視 14 if ( $this->input->method(TRUE) !== 'POST' ) return show_404(); 15 $json_str = $this->input->raw_input_stream; 16 //ここform_validationしやすい様に配列で渡す。 17 $json_data = json_decode($json_str, TRUE); 18 if ( empty($json_data) ) return show_404(); 19 20 $this->form_validation->set_data($json_data); 21 $this->form_validation->set_rules('name', 'lang:name', 'required|max_length[100]'); 22 $this->form_validation->set_rules('tel', 'lang:tel', 'required|is_natural|max_length[13]'); 23 $this->form_validation->set_rules('sex', 'lang:sex', 'required|in_list[1,2]'); 24 25 $result = new stdClass; 26 if ( $this->form_validation->run() === FALSE ) 27 { 28 $result->message = 'Request Validation Error'; 29 $result->statusCode = 'EA2'; 30 $result->validationMessages = $this->form_validation->error_array(); 31 return $this->output 32 ->set_content_type('application/json') 33 ->set_output(json_encode($result)); 34 } 35 36 if ( ! $this->api_model->add_user($json_data) ) 37 { 38 $result->message = 'User add Error'; 39 $result->statusCode = 'EA1'; 40 return $this->output 41 ->set_content_type('application/json') 42 ->set_output(json_encode($result)); 43 } 44 45 $result->statusCode = 'success!!!'; 46 $result->message = 'ユーザー登録できたっぽいよ!'; 47 return $this->output 48 ->set_content_type('application/json') 49 ->set_output(json_encode($result)); 50 } 51}
。。。。微妙。。。。
php
1<?php 2defined('BASEPATH') OR exit('のび太さんのエッチ!'); 3class Api extends CI_Controller{ 4 function __construct() 5 { 6 parent::__construct(); 7 $this->load->model('api_model'); 8 $this->load->library('form_validation'); 9 } 10 11 public function add_user() 12 { 13 $json_data = $this->validation_set_value(); 14 15 $result = $this->error_check($json_data) 16 if ( ! empty($result) ) 17 { 18 $result = new stdClass; 19 $result->statusCode = 'success!!!'; 20 $result->message = 'ユーザー登録できたっぽいよ!'; 21 } 22 return $this->show_output($result); 23 } 24 25 private function show_output(array $result) 26 { 27 return $this->output 28 ->set_content_type('application/json') 29 ->set_output(json_encode($result)); 30 } 31 32 private function error_check($json_data) 33 { 34 if ( $this->fvalidation_check($json_data) === FALSE ) 35 { 36 $result = $result = $this->err('Request Validation Error', 1); 37 } 38 elseif ( ! $this->api_model->add_user($json_data) ) 39 { 40 $result = $this->err('User add Error', 1); 41 } 42 $result; 43 } 44 45 private function err($msg, $code) 46 { 47 $result = new stdClass; 48 $result->statusCode = 'EA' . $e->getCode(); 49 $result->message = $e->getMessage(); 50 51 if ( ! empty(validation_errors()) ) 52 { 53 $result->validationMessages = $this->form_validation->error_array(); 54 } 55 return $result; 56 } 57 58 private function validation_check() 59 { 60 $this->form_validation->set_data($json_data); 61 $this->form_validation->set_rules('name', 'lang:name', 'required|max_length[100]'); 62 $this->form_validation->set_rules('tel', 'lang:tel', 'required|is_natural|max_length[13]'); 63 $this->form_validation->set_rules('sex', 'lang:sex', 'required|in_list[1,2]'); 64 return $this->form_validation->run(); 65 } 66 67 private function validation_set_value() 68 { 69 //POST Request以外は無視 70 if ( $this->input->method(TRUE) !== 'POST' ) return show_404(); 71 $json_str = $this->input->raw_input_stream; 72 //ここform_validationしやすい様に配列で渡す。 73 $json_data = json_decode($json_str, TRUE); 74 75 if ( empty($json_data) ) return show_404(); 76 return $json_data; 77 } 78}
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/09/03 01:27
2015/09/03 02:06
2015/09/03 03:37
2015/09/11 00:31