【问题标题】:PHP Fatal error: Cannot use try without catch or finally [duplicate]PHP致命错误:不能在没有catch或finally的情况下使用try [重复]
【发布时间】:2019-03-05 05:45:41
【问题描述】:

我的网页出现以下错误致命错误:无法使用 try without catch 或 finally 在第 13 行的 apply-for-job.php 中,我我自己找不到错误请求我需要帮助谢谢。下面是我的代码:-

<?php 
    include 'connect.php';
    if(isset($_POST['apply']))
    {
        $fname = $_POST['firstname'];
        $mname = $_POST['middlename'];
        $lname = $_POST['lastname'];
        $city = $_POST['city'];
        $state = $_POST['state'];
        $education = $_POST['education'];
        $vaccancy = $_POST['position'];

        try{

            $stmt = $db_con->prepare('INSERT INTO tbl_employment(firstName,middleName,lastName,userCity,userState,userEducation,userPosition) VALUES (:fname, :mname, :lname, :ucity, :ustate, :uedu, :uvacca)');
            $stmt->bindParam(":fname", $fname);
            $stmt->bindParam(":mname", $mname);
            $stmt->bindParam(":lname", $lname);
            $stmt->bindParam(":ucity", $city);
            $stmt->bindParam(":ustate", $state);
            $stmt->bindParam(":uedu", $education);
            $stmt->bindParam(":uvacca", $vaccancy);
            if ($stmt->execute())
            {
                $message="success";
            }
            else{
                $message="error";
            }
        }
    }

?>

我的第 13 行位于 try{ 所在的位置

【问题讨论】:

  • 错误信息不是那么含糊。任何try 块至少需要一个catchfinally,您应该在第30 行之后添加其中一个。
  • 你知道try的目的是什么吗?

标签: php


【解决方案1】:

您必须将catchtry 一起使用。请看php.net 手册。

PHP 具有类似于其他编程语言的异常模型。可以抛出异常,并在 PHP 中捕获(“捕获”)。代码可以被包围在一个 try 块中,以便于捕获潜在的异常。每次尝试必须至少有一个对应的 catch 或 finally 块。

应该是这样的:

try {
    print "this is our try block n";
    throw new Exception();
} catch (Exception $e) {
    print "something went wrong, caught yah! n";
} finally {
    print "this part is always executed n";
}

您不需要放置 finally 块,而是放置 catch 块。添加catch 块时,您的代码可能如下所示

<?php 
include 'connect.php';
if(isset($_POST['apply']))
{
    $fname = $_POST['firstname'];
    $mname = $_POST['middlename'];
    $lname = $_POST['lastname'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $education = $_POST['education'];
    $vaccancy = $_POST['position'];

    try{

        $stmt = $db_con->prepare('INSERT INTO tbl_employment(firstName,middleName,lastName,userCity,userState,userEducation,userPosition) VALUES (:fname, :mname, :lname, :ucity, :ustate, :uedu, :uvacca)');
        $stmt->bindParam(":fname", $fname);
        $stmt->bindParam(":mname", $mname);
        $stmt->bindParam(":lname", $lname);
        $stmt->bindParam(":ucity", $city);
        $stmt->bindParam(":ustate", $state);
        $stmt->bindParam(":uedu", $education);
        $stmt->bindParam(":uvacca", $vaccancy);
        if ($stmt->execute())
        {
            $message="success";
        }
        else
        {
            $message="error";
        }
    }
    catch(Exception $e)
    {
        // Do the necessary with exception 
    }
}

【讨论】:

  • 在 PHP 5.5 及更高版本中,也可以在 catch 块之后或代替 catch 块指定 finally 块。
【解决方案2】:

您需要捕获任何由 try 引发的异常

try{
    /* your code here */


} catch ( Exception $e ){
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

【讨论】:

    猜你喜欢
    • 2011-02-06
    • 2014-12-09
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    • 2012-11-15
    • 1970-01-01
    • 2017-03-27
    相关资源
    最近更新 更多