PHP:捕获级联异常。是否可以?

发布于 2024-10-23 16:25:26 字数 2202 浏览 3 评论 0原文

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

我怎样才能捕获级联异常? 我有一个函数( 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不再让梦枯萎 2024-10-30 16:25:27

只是不要在抛出异常的函数中捕获异常。然后它会被你的调用函数捕获。

Just don't catch the exception in the function where it is thrown. Then it'll be caught in your calling function.

不…忘初心 2024-10-30 16:25:27

这是 Exception 的基本行为:如果您不直接捕获异常,则该异常将被抛出到上层元素,依此类推到“根脚本”。

注意,一定要捕获异常,否则会报错。

您还可以从 catch 块“重新”抛出异常,如下所示:

catch(PDOException $e) {
    throw $e;
}

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:

catch(PDOException $e) {
    throw $e;
}
沐歌 2024-10-30 16:25:26

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

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

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

参考: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.

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

Reference : http://php.net/manual/en/language.exceptions.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文