PHP:捕获级联异常。是否可以?
我真的需要你的帮助...
我怎样才能捕获级联异常? 我有一个函数( A )调用另一个文件( B )中的另一个函数,该函数调用另一个文件( C )中的另一个函数。 我怎样才能捕获函数A的catch,函数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;
}
}
I really need your help...
How can I catch exception in cascade?
I have a function ( A ) that call another function in another file ( B ) that call another function in another file ( C ).
How can I get in the catch of the function A, the errors of the function C?
Is it possible?
Look my example below... I tried to be the most clear possible...
// 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只是不要在抛出异常的函数中捕获异常。然后它会被你的调用函数捕获。
Just don't catch the exception in the function where it is thrown. Then it'll be caught in your calling function.
这是 Exception 的基本行为:如果您不直接捕获异常,则该异常将被抛出到上层元素,依此类推到“根脚本”。
注意,一定要捕获异常,否则会报错。
您还可以从 catch 块“重新”抛出异常,如下所示:
This is the basic comportment of Exception : if you don't catch directly an exception this one is thrown to the upper element and so on to the "Root script".
Attention, you must catch the Exception, otherwise you'll get an error.
You can also "re"throwing an Exception from a catch block, like this:
如果你想级联异常,那么你需要再次抛出它们,而不是仅仅返回 false。
您可以为每个异常创建一个新的异常类型,也可以创建一个常规异常并设置适当的消息和代码。
参考:http://php.net/manual/en/language.exceptions.php
If you want to cascade the exceptions, then you need to throw them again instead of just returning false.
You can either create a new exception type for each of your exception, or your can create a general exception and set the appropriate message and code.
Reference : http://php.net/manual/en/language.exceptions.php