dongzouche9108 2013-05-24 08:18
浏览 32
已采纳

是否有可能有多个例外的Try / Catch Throw

i have the following code and i'm wondering if i can use try & catch as below:

class fun_database implements idbInfo{
    private $srvr=idbInfo::srvr_name;
    private $usr=idbInfo::usrnm;
    private $pass=idbInfo::psswrd;
    private $db=idbInfo::db_name;

    public function connct(){
        $hookup = new mysqli($this->srvr, $this->usr, $this->pass, $this->db);

        if ($hookup->connect_errno)     
        {
            throw new Exception("Error Processing Request", 1);
        }
    }

    public function sql_require_all($table_name, $table_col){
        $hookup = new connct();

        $result = $hookup->query("SELECT $table_col FROM $table_name");
            if($hookup->error()){
               throw new Exception("Error Processing Request", 1);
            }
        return $result->num_rows;
    }
}

This is a simple connection to the mysql and performing some querying there. Here is and the actual call of the functions above:

$conn = new fun_database();
    try{
        $result = $conn->sql_require_all('wordtypes', 'types');
    }
    catch(Exception $err){
        echo "Problems at:". $err->getMessage();
    }
    return "<option>".$result."</option>";

What i'm asking is a bit theory. Most probably this code is NOT WORKING (i didn't test it yet). I just want to know is it possible with one 'try' to 'catch' two exceptions (as you can see the first 'throw' is in the second method of fun_database, and the second 'throw' is in the first method of the same object which is only called from the second method).

sorry for making it too complicated but still can't figure it out id this structure of try/catch is working.

  • 写回答

6条回答 默认 最新

  • doureng5668 2013-05-24 08:23
    关注

    you can only catch different types of exception...

    class fun_database implements idbInfo{
    private $srvr=idbInfo::srvr_name;
    private $usr=idbInfo::usrnm;
    private $pass=idbInfo::psswrd;
    private $db=idbInfo::db_name;
    
    public function connct(){
        $hookup = new mysqli($this->srvr, $this->usr, $this->pass, $this->db);
    
        if ($hookup->connect_errno)     
        {
            throw new DomainException("Error Processing Request", 1);
        }
    }
    
    public function sql_require_all($table_name, $table_col){
        $hookup = new connct();
    
        $result = $hookup->query("SELECT $table_col FROM $table_name");
            if($hookup->error()){
               throw new Exception("Error Processing Request", 1);
            }
        return $result->num_rows;
    }
    }
    

    Then:

    try{
        $conn = new fun_database();
        $result = $conn->sql_require_all('wordtypes', 'types');
    }
    catch(DomainException $err){
        echo "This Problem at:". $err->getMessage();
    }
    catch(Exception $err){
        echo "That Problem at:". $err->getMessage();
    }
    return "<option>".$result."</option>";
    

    you would need your class instantiation inside that try block though I believe.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证