gpt4 book ai didi

php - PHP : catch exception in cascade. Is it possible?

转载 作者:行者123 更新时间:2023-12-03 07:45:31 24 4
gpt4 key购买 nike

我真的需要你的帮助...

如何在层叠中捕获异常?
我有一个函数(A),该函数在另一个文件(B)中调用另一个函数,在另一个文件(C)中调用另一个函数。
我怎样才能捕获函数A的错误,函数C的错误?
可能吗?
看下面的例子...我试图尽可能清晰...

// This code is in a file ( file A ) that call a function of a file B
require_once('/User_Login.php');

try
{
CREATE_NEW_User_Login(arr_Test, 'hello');
return true;
}
catch(Exception $e)
{
echo $e->getMessage();
return false;
}
}





// This code is in another file ( file B ) that call a function of a file C
public function CREATE_NEW_User_Login($_INPUT_Array = null, $_INPUT__Password = null)
{

// Db connection...


require_once('/User__DBclass.php');
$DBCl_User = new User($DBConn, null);
$DBCl_User->SET__CLASS_ATTRIBUTES_Value__by_Array($_INPUT_Array);
$DBCl_User->SETTER__user_pass(md5($_INPUT__Password));

$this->config['mysqli']->beginTransaction();

try
{
$DBCl_User->INSERT__NEW_ROW())
return true;
}
catch(Exception $e)
{
echo $e->getMessage();
return false;
}
}




// This code is in another file ( file C )
// In the User Class ( file: User__DBclass.php )
public function INSERT__NEW_ROW()
{
$this->config['stmt'] = $this->config['mysqli']->prepare('INSERT INTO tbl_user
SET user_id = :user_id,
user_name = :user_name,
act_code = :act_code;');


$this->config['stmt']->bindParam(':user_id', $this->user_id, PDO::PARAM_INT);
$this->config['stmt']->bindParam(':user_name', $this->user_name, PDO::PARAM_STR);
$this->config['stmt']->bindParam(':act_code', $this->act_code, PDO::PARAM_STR);


try
{
$this->config['stmt']->execute();
$this->user_id = intval($this->config['mysqli']->lastInsertId());
return $this->user_id;
}
catch(PDOException $e)
{
echo $e->getMessage();
return false;
}
}

最佳答案

如果要级联异常,则需要再次抛出它们,而不仅仅是返回false。

您可以为每个异常创建新的异常类型,也可以创建常规异常并设置适当的消息和代码。

 catch(Exception $e)
{
//do something here if you want
throw $e;
}

引用: http://php.net/manual/en/language.exceptions.php

关于php - PHP : catch exception in cascade. Is it possible?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5314721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com